【问题标题】:In R, how can I plot the location of item with their source on europe map using ggplot?在 R 中,如何使用 ggplot 在欧洲地图上绘制项目的位置及其来源?
【发布时间】:2019-11-20 07:56:40
【问题描述】:

我想绘制每个项目的位置(“经度”和“纬度”)及其“来源”(例如英语、法语、韩语或日语),如下图所示。

为此,我准备了一个包含“经度”、“纬度”和“来源”的“csv”文件,如下所示,这是人口样本。

latitude    longitude   source
51.318488   -1.0605415  English
51.4603395  -0.115406   Korean
47.9818605  0.195548    English
40.226213   28.954341   English
53.717188   27.978099   English
45.800371   9.0838175   French
41.292651   12.573501   French
41.8984165  12.5451455  Japanese
53.651504   -2.6340755  English
48.382929   31.181446   Korean
40.176071   29.121293   English
51.6471965  -0.7084465  English
40.54108    -3.63148    English
40.6280185  -8.6396565  French
51.5424365  -0.159403   French
36.89007    30.680889   Japanese
48.8588335  2.347003    English
52.238854   -0.882951   Korean
37.383198   -5.923545   English
45.080766   11.5820475  English
41.392657   2.1412275   English
41.0213205  29.0052225  French
53.0184905  -1.356315   French

我在这里尝试过,但没有成功。而从 map_data 中,我只能得到“世界”、“法国”和“国家”。

df <- read.csv("data", header =T, sep = ',')
world_map <- map_data("world")
ggplot(world_map, aes(x = longitude, y = latitude, group = group)) + geom_polygon(fill="lightgray", colour = "white") geom_polygon(aes( group = group, fill = region)) +
geom_point(data = df, aes(x = longitude, y = latitude, color = source), size = 1, alpha = 1/5, color = "darkblue")

theme(axis.line = element_blank(), 
  axis.text = element_blank(), 
  axis.ticks = element_blank(), 
  axis.title = element_blank(), 
  panel.background = element_blank(), 
  panel.border = element_blank(), 
  panel.grid.major = element_blank(), 
  panel.grid.minor = element_blank(), 
  plot.background = element_blank())

我怎样才能制作上面的第一张照片?

【问题讨论】:

  • 有几件事:您的代码中缺少+。对于与主题无关的问题,我们也不需要 9 行主题设置代码,尤其是因为您还缺少 + ,这会将其附加到实际情节中。至于国家/地区,该软件包并不包含每个国家/地区的 shapefile——其中包含的只是常见的参考。您必须找到自己想要的 shapefile;自然地球项目是一个很好的资源。请更具体地说明什么不起作用

标签: r ggplot2 maps


【解决方案1】:

使用您提供的数据样本,我可以在地图上找到一些东西。我没有注册的 google api,所以我根据您拥有的数据框手动限制了地图:

library(ggmap)
library(ggplot2)
library(ggthemes)

world_map <- map_data("world")
# so call map of europe
# will change according to what you have in df
world_map = subset(world_map,long < max(df$longitude )+5 & long > min(df$longitude)-5)
world_map = subset(world_map,lat < max(df$latitude)+5 & lat > min(df$latitude)-5)
# to get city names
LAB = world_map %>% 
group_by(region) %>% 
select(region,long,lat) %>% 
summarise_all(mean)

#plot
ggplot(world_map, aes(x = long, y = lat)) + 
geom_polygon(aes( group = group, fill = region),show.legend=FALSE,alpha=1/5) +
geom_point(data = df, aes(x = longitude, y = latitude, color = source), 
size = 1) + 
geom_text(data=LAB,aes(label=region),size=2)+
scale_fill_grey() +theme_map()

不是最好的,但如果你玩弄颜色等可能会变得更好

【讨论】:

    猜你喜欢
    • 2020-11-09
    • 2021-08-08
    • 2020-11-19
    • 2012-09-22
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多