geom_map在ggplot2中的边界 [英] geom_map borders in ggplot2

查看:152
本文介绍了geom_map在ggplot2中的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ggplot2中使用geom_map生成一个choropleth映射。我想用黑色或其他颜色勾勒不同区域,以区分梯度上颜色相似的区域。使用下面的代码我试图设置颜色来勾勒区域。该代码生成地图,但没有边界。当我将颜色命令移动到美学中时,它会用图例产生预期的粉红色边框。任何想法为什么设置颜色不会产生边界,但映射会?

I am trying to produce a choropleth map using geom_map in ggplot2. I want to outline the various regions in black or some other color to distinguish between areas that are similar in color on the gradient. Using the following code I've tried to set the color to outline the areas. The code produces the map, but no borders. When I move the colour command into the aesthetic it produces the expected 'pink' borders with a legend. Any ideas why setting the color won't produce borders, but mapping will? I saw a similar discussion on the ggplot2 Google Group.

ggplot(subset(df, as.character(variable) == "value"), aes(map_id = id)) +
  geom_map(aes(fill = pct), colour = "black", map = ggmap) +
  expand_limits(x = ggmap$long, y = ggmap$lat) +
  scale_fill_gradient(low = "antiquewhite", high = "darkred") +
  opts(title = "Title", panel.background = theme_rect(fill = "grey90"))

谢谢

Thanks

推荐答案

您需要使用geom_polygon添加多边形边框。在下面的代码中,你需要用一个数据集( data )和lat和long>(> XXXX code> x 和 y )每个多边形的值。我通常从 maps 包中获取数据,不确定这是否是您所做的。

You need to add the polygon borders using geom_polygon. In the code below you need to fill in the XXXX with a data set (data) and lat and long (x and y) values for each polygon. I usually get my data from the maps package, not sure if this is what you did.

ggplot(subset(df, as.character(variable) == "value"), aes(map_id = id)) +
  geom_map(aes(fill = pct), colour = "black", map = ggmap) +
  geom_polygon(data=XXXX, aes(x=XXXX, y=XXXX), colour='black', fill=NA) +
  expand_limits(x = ggmap$long, y = ggmap$lat) +
  scale_fill_gradient(low = "antiquewhite", high = "darkred") +
  opts(title = "Title", panel.background = theme_rect(fill = "grey90"))

这篇关于geom_map在ggplot2中的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆