图例中包含气泡图大小的科幻地图

时间:2019-12-06 19:38:41

标签: r ggplot2 sf

我可以使用geom_point绘制地图,该地图绘制每个点的大小,颜色和alpha随人口而变化(请参考1)。图例正确显示了与我为总体定义的每个中断相关的大小,颜色和Alpha。但是,如果我改用geom_sf绘制点(所有其他代码相同),则图例不会正确显示。它仅显示一个矩形框,其中的线条已调整为颜色,而alpha或气泡大小没有变化(请参阅2)。有关如何解决此问题的任何想法?帖子底部的代码

使用geom_point传奇式显示气泡大小 Using geom_point

使用geom_sf显示气泡大小 Using geom_sf

# Code using geom_point    
data %>%
  ggplot() +
  geom_sf(data = map, aes(geometry=geometry), fill='#d5d5d2', colour=NA) +
  geom_point(aes(x=longitude, y=latitude, size=popM, color=popM, alpha=popM),
         shape=20, stroke=FALSE) +
  scale_size_continuous(name='Population (M)', trans='log',
                        range=c(1,12), breaks=mybreaks) +
  scale_alpha_continuous(name='Population (M)', trans='log',
                         range=c(0.1, .9), breaks=mybreaks) +
  scale_color_viridis(option="magma", trans="log", breaks=mybreaks,
                      name='Population (M)') +
  theme_void() + coord_sf() + 
  guides( colour = guide_legend()) +
  ggtitle('Cities With A Population Over 20 Thousand') +
  theme(
    legend.position = c(0.1, 0.25),
    text = element_text(color = '#22211d'),
    plot.background = element_rect(fill = '#f5f5f2', color = NA), 
    panel.background = element_rect(fill = '#f5f5f2', color = NA), 
    legend.background = element_rect(fill = '#f5f5f2', color = NA),
    plot.title = element_text(size= 16, hjust=0.1, color = '#2e2d27',
                              margin = margin(b = -0.1, t = 0.4, l = 2,
                                              unit = 'cm'),
                              face = 'bold'),
  )

# For the second map, geom_point is replaced with this line
geom_sf(aes(geometry=geometry, size=popM, color=popM, alpha=popM),
             shape=20, stroke=FALSE) +

0 个答案:

没有答案