ggplot2地图中纽约州的多边形不正确 [英] Incorrect polygon for New York State in ggplot2 maps
问题描述
我正在尝试在纽约州地图上绘制数据.我正在使用map_data代码.但是,如果您查看多边形,它会显示出实际上不属于纽约州的一部分吗?有什么想法可以对地图数据应用过滤器以将其删除吗?
ny<-map_data("state",region =纽约")s1<-ggplot()+ geom_polygon(data = ny,aes(x = long,y = lat))s2<-ggplot()+ geom_point(data = ny,aes(x = long,y = lat))grid.arrange(s1,s2,ncol = 2)
输出:geom_point显示正确的边界,但不显示多边形
状态实际上是由多个未连接的多边形组成.您只需要告诉ggplot哪些点与哪些组一起使用即可.这是通过将数据映射到 aes()
的 group
参数来完成的.请参阅文档此处,但如果有地图示例会更好./p>
那么,您如何知道哪些点与哪些组一起使用? map_data()
返回的数据帧包含一个 group
列.参见:
head(ny)ny $ group
要正确绘制地图,请使用:
ggplot()+ geom_polygon(data = ny,aes(x = long,y = lat,group = group))
I am trying to plot data on New York state map. I am using map_data code. But If you look at polygon, It shows extra piece which is actually not part of New York state? Any ideas how can I apply filter on map data to remove that?
ny <- map_data("state", region="new york")
s1 <- ggplot() + geom_polygon(data=ny, aes(x=long, y=lat))
s2 <- ggplot() + geom_point(data=ny, aes(x=long, y=lat))
grid.arrange(s1, s2, ncol=2)
Output: geom_point shows correct boundary, but not polygon
The state is actually composed of multiple polygons which are not connected. You just need to tell ggplot which points go with which groups. This is done by mapping your data to the group
argument of aes()
. See the documentation here, although it would be nicer if they had a map example.
So how do you know which points go with which groups? The data frame returned by map_data()
contains a group
column. See:
head(ny)
ny$group
To plot the map correctly, use:
ggplot() + geom_polygon(data = ny, aes(x = long, y = lat, group = group))
这篇关于ggplot2地图中纽约州的多边形不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!