ggplot2中的geom_map边框 - 重新访问

yel*_*cap 5 r map ggplot2

这个问题上我和@Mike有类似的问题.问题是如何在地图中设置区域的轮廓颜色.

建议的解决方案是geom_polygon在边界上添加一个绘图.只要绘制整个区域,这就有效.当尝试限制到子区域时,多边形被奇怪地绘制(可能是因为一些顶点被丢弃).使用标准geom_map示例:

# Create example data
ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
values <- data.frame(id = ids, value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5))
positions <- data.frame(
  id = rep(ids, each = 4),
  x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3, 0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
  y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5, 2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
)

# Plot data
ggplot(values, aes(fill = value)) + 
    geom_map(aes(map_id = id), map = positions) +
    geom_polygon(aes(x,y,group=id), fill = NA, colour = 'red', data = positions) +
    expand_limits(positions) +
    ylim(0, 3)
Run Code Online (Sandbox Code Playgroud)

一种可能的解决方法是使用颜色美学geom_map,然后手动选择轮廓颜色scale_colour_manual,如下所示:

ggplot(values, aes(fill = value)) + 
    geom_map(aes(map_id = id, colour = 'white'), map = positions) +
    scale_colour_manual(values=c('white')) +
    expand_limits(positions) +
    ylim(0, 3)
Run Code Online (Sandbox Code Playgroud)

所以我有两个问题:

  1. geom_polygon当轴限制受到限制时,为什么不能正常工作?
  2. 是否有一个更优雅的解决方案为轮廓着色而不是在这里展示的?

以下是情节输出.提前谢谢了.

使用geom_polygon无法正常工作 工作但不是很优雅

jor*_*ran 8

我相信你为什么不起作用是正确的.在绘图之前使用xlimylim剪辑数据限制x或y限制.这将最终省略多边形中的一些顶点,因此有些东西不会被绘制.

这就是为什么coord_cartesian允许您调整x和y限制而不剪切数据的原因.它将"缩放"到正确的区域,而不是剪切然后绘图.

所以不要ylim试试+ coord_cartesian(ylim = c(0,3)).