在 R 中使用 ggplot 为地图边界绘制的线条太多
Too many lines drawn for map borders using ggplot in R
我正在使用 ggplot 创建地图,并根据我的数据使用渐变填充不同区域。
默认情况下没有绘制边界线的地图。我想包括它们,并根据 this question 编写了一些代码。然而,这导致了太多的线,我认为它连接了地区的所有角落。我该如何避免这种情况?
在下面的代码中,datafile 是我想要在地图上显示的数据的存储位置,Scot 是 shapefile。
导致问题的行是 geom_polygon。
ggplot() +
geom_map(data = datafile, aes(map_id = region, fill = datafile$"2007"), map = Scot) +
geom_polygon(data = Scot, aes(x = Scot$long, y = Scot$lat), colour = "gray", fill = NA) +
expand_limits(x = Scot$long, y=Scot$lat) +
scale_fill_gradient(low = ("lightyellow"), high = ("red"), limits = c(0,35000)) +
ggtitle("2007") +
coord_fixed(1.2) +
theme(axis.text.x = element_blank(), axis.text.y = element_blank(),
axis.ticks = element_blank(), axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.border = element_blank(), panel.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(face = "bold", hjust = 0.5))
尝试将 group = group
添加到 geom_polygon 行。正如 Richard Telford 所说,您不必在 aes
中使用 $ 符号,因为您已经通过 data = Scot
:
指示了数据源
... + geom_polygon(data = Scot, aes(x = long, y = lat, group = group),
colour = "gray", fill = NA)
注意:我假设 Scot 数据框是通过强化某种空间数据集获得的,它总是包含一个名为 "group" 的列。如果不存在,请查找指示哪些点应属于同一多边形的列。 geom_polygon
的帮助文件指出(强调):
Polygons are very similar to paths (as drawn by geom_path) except that
the start and end points are connected and the inside is coloured by
fill. The group aesthetic determines which cases are connected
together into a polygon.
我正在使用 ggplot 创建地图,并根据我的数据使用渐变填充不同区域。
默认情况下没有绘制边界线的地图。我想包括它们,并根据 this question 编写了一些代码。然而,这导致了太多的线,我认为它连接了地区的所有角落。我该如何避免这种情况?
在下面的代码中,datafile 是我想要在地图上显示的数据的存储位置,Scot 是 shapefile。
导致问题的行是 geom_polygon。
ggplot() +
geom_map(data = datafile, aes(map_id = region, fill = datafile$"2007"), map = Scot) +
geom_polygon(data = Scot, aes(x = Scot$long, y = Scot$lat), colour = "gray", fill = NA) +
expand_limits(x = Scot$long, y=Scot$lat) +
scale_fill_gradient(low = ("lightyellow"), high = ("red"), limits = c(0,35000)) +
ggtitle("2007") +
coord_fixed(1.2) +
theme(axis.text.x = element_blank(), axis.text.y = element_blank(),
axis.ticks = element_blank(), axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.border = element_blank(), panel.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(face = "bold", hjust = 0.5))
尝试将 group = group
添加到 geom_polygon 行。正如 Richard Telford 所说,您不必在 aes
中使用 $ 符号,因为您已经通过 data = Scot
:
... + geom_polygon(data = Scot, aes(x = long, y = lat, group = group),
colour = "gray", fill = NA)
注意:我假设 Scot 数据框是通过强化某种空间数据集获得的,它总是包含一个名为 "group" 的列。如果不存在,请查找指示哪些点应属于同一多边形的列。 geom_polygon
的帮助文件指出(强调):
Polygons are very similar to paths (as drawn by geom_path) except that the start and end points are connected and the inside is coloured by fill. The group aesthetic determines which cases are connected together into a polygon.