我试图在 ggplot map 上绘制我所有的网站。我有 5 个位置,每个位置有多个站点,但是当我尝试绘制所有站点时,它只显示每个位置一个点,而不是每个站点一个点。我有 34 个站点和 5 个位置,所以我需要一张有 34 个点而不是 5 个点的 map !任何帮助将不胜感激!非常感谢
这些是 y 和 x 作为我的经度和纬度的数据。
Location PAR y x Guam GFA 201.09952 144.8786111 13.495 Guam GFA2 179.04171 144.6597222 13.41638889 Guam GFA3 67.66379 144.2761111 13.47333333 Guam GFB1 201.09952 144.7105556 13.31416667 Guam GFB2 179.04171 144.655 13.50194444 Guam GFB3 67.66379 144.8697222 13.37472222
我需要为关岛绘制所有 6 个站点。这是我正在使用的代码和最终输出
map.world <- map_data(map="world")
gg <- ggplot()
gg <- gg + theme(legend.position="none")
gg <- gg + geom_map(data=map.world, map=map.world, aes(map_id=region, x=long, y=lat), fill="white", colour="black", size=0.25) + theme_bw()
gg
par<-read.csv("parmap.csv", header=T)
head(par)
g<-gg+ geom_polygon() +
geom_point(data=par, aes(x = y, y = x, color=PAR)) +theme_minimal()
g
最佳答案
在这种情况下,您可能需要在兴趣点附近缩放 map ,即“关岛”。
一种方法可能如下所示
library(ggplot2)
library(ggmap)
#get the map
center = paste(min(df$lat)+(max(df$lat)-min(df$lat))/2,
min(df$lon)+(max(df$lon)-min(df$lon))/2, sep=" ")
map <- get_map(location = center, maptype = "roadmap", zoom = 8, source = "google", color="bw")
p <- ggmap(map)
#plot points on the map
plt <- p +
geom_point(data=df, aes(x = lon, y = lat, color = PAR)) +
scale_color_gradientn(colours = rainbow(10)) +
theme_minimal()
plt
更新:我稍微修改了您的示例数据。您会注意到,我在示例数据中又添加了一个位置以在 map 上绘制所有 7 个点(即一个位置的 6 个站点和另一个位置的 1 个站点)。
输出图为:
注意:如果您收到
geocode failed with status OVER_QUERY_LIMIT
然后出错 this链接可能会有所帮助。样本数据:
df <- structure(list(Location = c("Guam", "Guam", "Guam", "Guam", "Guam",
"Guam", "N_Mariana_Islands"), sites = c("GFA", "GFA2", "GFA3",
"GFB1", "GFB2", "GFB3", "NMI1"), PAR = c(201.09952, 179.04171,
67.66379, 201.09952, 179.04171, 67.66379, 100), lon = c(144.8786111,
144.6597222, 144.2761111, 144.7105556, 144.655, 144.8697222,
145.192522), lat = c(13.495, 13.41638889, 13.47333333, 13.31416667,
13.50194444, 13.37472222, 14.150817)), .Names = c("Location",
"sites", "PAR", "lon", "lat"), class = "data.frame", row.names = c(NA,
-7L))
# Location sites PAR lon lat
#1 Guam GFA 201.09952 144.8786 13.49500
#2 Guam GFA2 179.04171 144.6597 13.41639
#3 Guam GFA3 67.66379 144.2761 13.47333
#4 Guam GFB1 201.09952 144.7106 13.31417
#5 Guam GFB2 179.04171 144.6550 13.50194
#6 Guam GFB3 67.66379 144.8697 13.37472
#7 N_Mariana_Islands NMI1 100.00000 145.1925 14.15082
关于r - ggplot2 map 上的多个站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50082984/