【问题标题】:multiple sites on a ggplot2 mapggplot2 地图上的多个站点
【发布时间】:2018-04-30 18:23:26
【问题描述】:

我正在尝试在 ggplot 地图上绘制我的所有网站。我有 5 个位置,每个位置有多个站点,但是当我尝试绘制所有站点时,它只显示每个位置一个点,而不是每个站点一个点。我有 34 个站点和 5 个位置,所以我需要一个 34 个点而不是 5 个点的地图!任何帮助将不胜感激!非常感谢

这些是 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

【问题讨论】:

  • @Sathish 非常感谢您编辑我的代码
  • 使用您发布的示例数据,绘制了所有六个站点。它们非常靠近,以至于当您绘制完整的世界地图时,这些点会出现在彼此的顶部。
  • 哦,非常感谢!那么应该如何编辑来显示网站呢?

标签: r ggplot2


【解决方案1】:

您可能需要在兴趣点附近缩放地图,在这种情况下为“关岛”。

一种方法可能如下所示

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

更新:我稍微修改了您的示例数据。您可以注意到,我在示例数据中添加了一个位置以在地图上绘制所有 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

【讨论】:

  • 非常感谢@Prem! GF实际上是我的网站!所以它一直按站点而不是按 PAR 着色点......它不会让我从其他位置绘制站点......对不起,我对此很陌生
  • 我如何最终放大我创建的地图以显示我的所有点?谢谢
猜你喜欢
  • 2011-09-19
  • 2017-02-04
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 2016-04-27
相关资源
最近更新 更多