【问题标题】:Is it possible to overlay a scatterplot on map using ggplot?是否可以使用 ggplot 在地图上叠加散点图?
【发布时间】:2016-03-24 01:50:22
【问题描述】:

我想画一张法国省(即法国的地区)的地图并在地图上标点。我使用 ggplot2 绘制地图,但是当我想在地图上添加点时,R 返回一个错误,指出它找不到“组”。

library("ggplot2")
library("ggmap")

# load the contour of French departements
wg  <- read.csv("data/departements.csv")

metropoles  <- c("Paris", "Rennes", "Rouen", "Lille", "Marseille")
geo  <- geocode(metropoles)
met  <- data.frame(ville = metropoles, geo)

map  <- ggplot(data = wg, aes(x = long, y = lat, group = group)) + 
geom_polygon() + 
scale_x_continuous(limits = c(-7,10)) + 
scale_y_continuous(limits = c(40,53)) + 
coord_map() + 
theme(axis.text = element_blank(), axis.title = element_blank()) 

map + geom_point(data = met, aes(x = lon, y = lat))

对于复制,您可以找到原始数据 here 和 R 程序 here

【问题讨论】:

  • map + geom_point(data = met, aes(x = lon, y = lat, group=NULL))
  • 谢谢。你知道我们为什么要添加这个选项吗?
  • 您在“全局”aes 中定义了它,但met 中没有group 列。

标签: r ggplot2


【解决方案1】:

@Roland 给出了很好的答案。问题是我将 group 定义为一般论点。

ggplot() + 
  geom_polygon(data = wg, aes(x = long, y = lat, group = group)) + 
  scale_x_continuous(limits = c(-7,10)) + 
  scale_y_continuous(limits = c(40,53)) + 
  coord_map() + 
  theme(axis.text = element_blank(), axis.title = element_blank()) 
  geom_point(data = met, aes(x = lon, y = lat))

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多