簡體   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