使用 gganimate 时 ggplot geom_polygon 的奇怪行为

bil*_*999 5 plot r ggplot2 gganimate

说我有这些数据。一个用于对州的县进行着色,另一个用于绘制点。

library(tidyverse)
library(gganimate)
devtools::install_github("UrbanInstitute/urbnmapr")
library(urbanmapr)

#1. counties dataset for the shading of the background
data(counties)
#keep only Texas counties
counties <- filter(counties, state_fips==48)

#2. dots dataset for dots over time
dots <- data.frame(group = c(rep(1,3), rep(2,3)), 
                    lat = c(rep(32, 3), rep(33, 3)),
                    long = c(rep(-100, 3), rep(-99,3)), 
                    year = c(1:3, 1:3))
Run Code Online (Sandbox Code Playgroud)

然后我绘制它们(首先不使用gganimate):

ggplot() +
    geom_polygon(data = counties, aes(long, lat, group = county_fips, fill = as.numeric(county_fips))) +
    scale_fill_gradient(low = "navy", high = "lightskyblue3")   +   
    geom_point(data = dots, aes(long, lat, group=interaction(long, lat), color=year), 
               size=6, show.legend = FALSE) + 
    theme_bw() +
    scale_color_gradientn(colours = c("red", "yellow", "darkgreen")) +
    coord_map() +
    labs(subtitle = paste('Year: {frame_time}')) +  
    theme(plot.subtitle = element_text(hjust = 0.8, vjust=-10, size=30)) +  
    theme(panel.background = element_rect(fill = 'white')) +
    theme(panel.grid = element_blank(),axis.title = element_blank(),
          axis.text = element_blank(),axis.ticks = element_blank(),
          panel.border = element_blank())+
    theme(legend.position = c(0.15, .15)) +
    theme(legend.key.size = unit(2,"line"),legend.title=element_text(size=16), 
          legend.text=element_text(size=14))    +
    labs(fill = "abc")
Run Code Online (Sandbox Code Playgroud)

结果如下:

在此处输入图片说明

当我尝试使用gganimate以下方法为点设置动画时,问题就出现了:

map <- ggplot() +
    geom_polygon(data = counties, aes(long, lat, group = county_fips, fill = as.numeric(county_fips))) +
    scale_fill_gradient(low = "navy", high = "lightskyblue3")   +
    geom_point(data = dots, aes(long, lat, group=interaction(long, lat), color=year), 
               size=6, show.legend = FALSE) + 
    theme_bw() +
    scale_color_gradientn(colours = c("red", "yellow", "darkgreen")) +
    coord_map() +
    labs(subtitle = paste('Year: {frame_time}')) +  
    theme(plot.subtitle = element_text(hjust = 0.8, vjust=-10, size=30)) +  
    theme(panel.background = element_rect(fill = 'white')) +
    theme(panel.grid = element_blank(),axis.title = element_blank(),
          axis.text = element_blank(),axis.ticks = element_blank(),
          panel.border = element_blank())+
    theme(legend.position = c(0.15, .15)) +
    theme(legend.key.size = unit(2,"line"),legend.title=element_text(size=16), 
          legend.text=element_text(size=14))    +
    labs(fill = "abc") +    
    transition_time(year) +
    shadow_mark(size=6)     

anim_save("output/test.gif", map, end_pause=6, width = 800, height = 800, duration=8)
Run Code Online (Sandbox Code Playgroud)

请注意,除了transition_timeshadow_mark行之外,它完全相同。结果如下:

在此处输入图片说明

背景颜色非常不同。可能是颜色颠倒了之类的;我不确定。然而,在我使用更多点和不同阴影值的真实示例中,阴影实际数据几乎没有相似之处。世界上发生了什么,我该如何解决?

Cla*_*lke 3

我不确定问题到底是什么,但多边形可能会出现问题,因为它们往往违反“每个观察一行”的整洁数据规则。另外,作为一般原则,我建议geom_sf()您在处理地理空间数据时使用它。下面是使用 重现动画的代码geom_sf()

理论上,我也应该能够用 来绘制点geom_sf(),但是我遇到了另一个问题,看起来像是 gganimate 的错误,所以我必须解决并提取点的投影坐标并用 来绘制geom_point()

library(tidyverse)
library(gganimate)
library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0

# 1. get shape files for texas
library(tidycensus)
options(tigris_use_cache = TRUE)

# download counties for Texas
counties <- get_acs(
  state = 48, # omit to download all US counties
  geography = "county", year = 2015, geometry = TRUE,
  variables = "B19013_001" # need to specify some variable to download, here median income
)
#> Getting data from the 2011-2015 5-year ACS

# 2. dots dataset for dots over time
dots <- data.frame(
  group = c(rep(1,3), rep(2,3)), 
  lat = c(rep(32, 3), rep(33, 3)),
  long = c(rep(-100, 3), rep(-99,3)), 
  year = c(1:3, 1:3)
)

# add geometry column
dots_sf <- dots %>%
  mutate(
    geometry = st_sfc(
      map2(lat, long, function(lat, long) st_point(x = c(long, lat))),
      crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
    ) %>%
      st_transform(crs = 3083) # CRS appropriate for Texas, https://epsg.io/3083
  ) %>%
  st_as_sf()

# and extract transformed coords
dots_sf_transf <- dots_sf %>%
  mutate(
    long_transf = map_dbl(geometry, function(x) x[[1]]),
    lat_transf = map_dbl(geometry, function(x) x[[2]])
  )


# 3. static plot
p <- ggplot() +
  geom_sf(data = counties, aes(fill = as.numeric(GEOID))) +
  scale_fill_gradient(low = "navy", high = "lightskyblue3")   +   
  geom_point(
    data = dots_sf_transf, 
    aes(x = long_transf, y = lat_transf, group = group, color = year),
    size = 6, show.legend = FALSE
  ) + 
  scale_color_gradientn(colours = c("red", "yellow", "darkgreen")) +
  coord_sf(crs = 3083) +
  labs(subtitle = "Year: {frame_time}", fill = "abc") +  
  theme_void() +
  theme(legend.position = c(0.15, .15))
p
Run Code Online (Sandbox Code Playgroud)

# animation
p + transition_time(year) + shadow_mark(size=12)  
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2019-11-03 创建