繁体   English   中英

ggplot2 scale_fill_gradient 不一致消失

[英]ggplot2 scale_fill_gradient disappears inconsistently

我一直在学习如何为显示 covid-19 感染数据的工作构建地图。 除了国家地图,我还在制作东北、南部、西部和中西部的区域地图。 代码是相同的,我只是在国家数据框上使用了不同的过滤。 我为每天的数据制作一张地图,并使用 gganimate 制作框架,最后使用 gifski 制作动画 gif。 国家地图的基本代码是:

p <- pop_counties_cov %>%
  ggplot() +
  geom_sf(mapping = aes(fill = infRate, geometry=geometry), color = NA) +
  geom_sf(data = states_sf, fill = NA, color = "black", size = 0.25) +
  coord_sf(datum = NA) +   
  scale_fill_gradient(name = "% Population Infected", trans = "log", low='green', high='red',
                      na.value = "white",
                      breaks=c(0, round(max(pop_counties_cov$infRate),3))) +
  geom_point(data=AFMCbases, aes(x=longitude.1, y=latitude.1,size=personnel), color = "hotpink") +
  #geom_label_repel(data=AFMCbases, aes(x=longitude.1, y=latitude.1, label=Base)) +
  theme_bw() + 
  labs(size='AFMC \nMil + Civ') +
  theme(legend.position="bottom", 
        panel.border = element_blank(),
        axis.title.x=element_blank(), 
        axis.title.y=element_blank())

例如,它产生的最终帧是

在此处输入图片说明

另一方面,此代码:

p <- mw_pop_counties_cov %>%
  ggplot() +
  geom_sf(mapping = aes(fill = infRate, geometry=geometry), color = NA) +
  geom_sf(data = mw_states_sf, fill = NA, color = "black", size = 0.25) +
  coord_sf(datum = NA) +   
  scale_fill_gradient(name = "% Population Infected", trans = "log", low='green', high='red',
                      na.value = "white",
                      breaks=c(0, round(max(mw_pop_counties_cov$infRate),3))) +
  geom_point(data=mwBases, aes(x=longitude.1, y=latitude.1,size=personnel), color = "hotpink") +
  #geom_label_repel(data=AFMCbases, aes(x=longitude.1, y=latitude.1, label=Base)) +
  theme_bw() + 
  labs(size='AFMC \nMil + Civ') +
  theme(legend.position="bottom", 
        panel.border = element_blank(),
        axis.title.x=element_blank(), 
        axis.title.y=element_blank())

这是相同的,除了数据帧已被过滤到仅中西部州,产生

在此处输入图片说明

注意色标的外观。

是否有一些错别字我没有看到,因为我盯着这个看得太久了?
我的脚本生成了 5 个动画,每个用于国家地图,然后是 4 个人口普查区域(东北、中西部、南部和西部)。 色标出现在西部和中西部地图上,但没有出现在其他三个地图上。 尽管事实上我基本上只是剪切和粘贴然后更改了数据框名称。

我究竟做错了什么? 我希望色阶出现在所有地图上。

原来问题似乎是输出图上的水平空间之一。 通过简单地改变比例标题

scale_fill_gradient(name = "% Population Infected",...

scale_fill_gradient(name = "% Population \nInfected",

注意换行符\\n

输出贴图现在具有我正在寻找的色阶

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM