首页 > 解决方案 > 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)

This works as expected:

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?

标签: rggplot2mappinggis

解决方案


@TinglTanglBob 提供的答案

使用inherit.aes=FALSE 来防止geom_map 中定义的原始美学与geom_point 中给出的新aes 之间的混淆,


推荐阅读