问题描述
我有一个数据框架,其中包含诸如以下内容的鸣叫坐标:
I have a data frame with coordinates of tweets such as:
library(ggplot2)
df <- data.frame(long = c(-58.1, -58.2, -58.3, -58.4, -58.5, -55),
lat = c(-34.1, -34.2, -34.3, -34.4, -34.5, -25))
我想绘制布宜诺斯艾利斯的大都市区,即AMBA.它是由区域定义的:纵向:(-58,-59)纬度:(-34,-35)
I would like to plot the Metropolitan area of buenos aires, known as AMBA.It's defined by the area:longitud: (-58, -59)latitude: (-34, -35)
我的数据框中有一行位于AMBA区域之外. (对于问题(2)来说很重要)
I have a row in my data frame that's outside the AMBA area. (important for question (2))
con <- url("http://gadm.org/data/rda/ARG_adm2.RData")
print(load(con))
close(con)
ggmap <- fortify(gadm, region = "NAME_2")
设置情节限制,仅包括AMBA
lim <- data.frame(lon = c(-59, -58), lat = c(-35, -34))
情节
ggplot(data=ggmap, aes(x=long, y=lat)) +
scale_x_continuous(limits = c(-59,-58)) +
scale_y_continuous(limits = c(-35,-34)) +
geom_polygon(data = ggmap, fill = "grey80", aes(group=group)) +
geom_path(color="white",aes(group=group)) +
geom_point(data = df, aes(x = lon, y = lat, colour = "red"), alpha = 30/100)
问题:
- 主要问题是外部国家的政治边界区域不完整,因此地图看起来很奇怪.有办法解决这个问题吗?
- 我应该对数据框进行子集化,以便将观察结果保留在AMBA区域内,还是可以直接进行绘图并选择我直接感兴趣的区域.我相信这就是scale_x_continuous(limits = ...)的作用.
推荐答案
尝试使用coord_map
代替scale_x_continuous
/scale_y_continuous
.在坐标系上设置极限将缩放绘图(就像您正在用放大镜查看它一样),并且不会像在比例尺上设置极限那样更改基础数据.
Try coord_map
instead of scale_x_continuous
/scale_y_continuous
.Setting limits on the coordinate system will zoom the plot (like you're looking at it with a magnifying glass), and will not change the underlying data like setting limits on a scale will.
ggplot(data=ggmap, aes(x=long, y=lat)) +
geom_polygon(data=ggmap, fill="grey80", aes(group=group)) +
geom_path(color="white",aes(group=group)) +
geom_point(data=df, aes(x=long, y=lat), colour="red", alpha=30/100) +
coord_map(xlim=-c(59, 58), ylim=-c(35,34))
这篇关于使用geom_polygon绘制地图的地理边界不完整-ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!