r - I cannot add geom_point on top of an existing geom_map, "differing number of rows"?
问题描述
I am trying to make a map using ggplot2 and geom_map, but I am having trouble doing what should be an easy thing... adding a set of coordinates on top!
Here is my first bit of the code for the base map, based upon a shapefile I already have loaded in as mapbasis
:
p1<-ggplot(mapbasis,aes(long,lat,map_id=id,fill=id))+
geom_map(map=mapbasis)
Just to test the code I now create a data frame with just some coordinates:
test<-data.frame(pointid=c(1,2,3),lat=c(53, 51, 50),lon=c(-0.2, -0.1, -1.4))
I then try to add the points as follows:
p1+geom_point(aes(x=lon,y=lat),size=10,data=test)
I then, rather than having a map with three points on it, end up with the error message:
Error in data.frame ... arguments imply differing number of rows: 3,0
What is going on? I clearly have both longitude and latitude coordinates. I have also tried plotting just the test
object and it works fine.
Can anyone suggest what might be wrong?
解决方案
@TinglTanglBob 提供的答案
使用inherit.aes=FALSE 来防止geom_map 中定义的原始美学与geom_point 中给出的新aes 之间的混淆,
推荐阅读
- javascript - 将一个数组中的缺失元素插入另一个对象数组
- html - 如何修复我的图像中途中断?
- angular - 从 Angular 10 中的另一个组件获取价值
- java - 用静态源码分析工具(最好是Spoon)打印java方法参数的字面值
- android - 在 android 11 中未检测到 ARCore kotlin/flutter
- c++ - c++中的字符矩阵
- docker - 如何让我的证书适用于 LocalStack 中的 SSL
- go - 如何运行 GO 可执行文件?
- python - 在 Jupiter notebook 中运行“import torch”时,我得到“No module named “torch”
- c - 打印非 0 终止字符串时使用 percison 的可移植性