本文介绍了是否可以使用ggplot在地图上叠加散点图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想画一张法国部门的地图(即法国的地区),并在地图上标出点.我使用ggplot2绘制地图,但是当我想在地图上添加点时,R返回错误,提示它找不到组".
I would like to draw a map of French departements (ie districts in France) and plot points on the map. I use ggplot2 to draw the map but when I want to add points on the map, R returns an error which says that it cannot find 'group'.
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))
For replication, you can find the raw data here and the R program here
推荐答案
@Roland的回答很好.问题是我已经将group定义为通用参数.
@Roland had the good answer. The problem is that I had defined group as a general argument.
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))
这篇关于是否可以使用ggplot在地图上叠加散点图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!