Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot gives "arguments imply differing number of rows" error in geom_point while it isn't true - how to debug?

Tags:

r

ggplot2

spatial

I have two objects of type Large SpatialPointsDataFrame named st2 and st10. They come from the same source. They both plot without problems with:

plot(st2)

or

plot(st10)

But I want to plot them with ggmap and ggplot. I can do this for st2 with a simple code like this:

map <- get_map(location = 'Poznań', zoom = 12)
ggmap(map) + geom_point(aes(x =st2@coords[,1], y = st2@coords[,2]))

But when it comes to st10 I get an error:

Error in data.frame(x = c(16.910848618, 16.910863876, 16.910913467, 16.910936356,  :   
arguments imply differing number of rows: 53885, 4

I check the values with length():

> length(st10@coords[,1])
[1] 53885
> length(st10@coords[,2])
[1] 53885

I check them with summary()

> summary(st10@coords[,1])
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  16.84   16.88   16.91   16.91   16.91   16.99 
> summary(st10@coords[,2])
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  52.35   52.41   52.46   52.44   52.46   52.46 

What is wrong? I have over 20 of those SP data frames and some of the plot well other give the error mentioned above...this is not related to number of points

What can be wrong? Or maybe someone can give me some tips how can i debug this?

like image 928
mrz Avatar asked Oct 24 '14 19:10

mrz


1 Answers

I got around this by making a new data frame.

tlData=data.frame(x=c(0,100000),y=c(0,1000000)
ggplot(otherdf,aes(x=X,y=Y)+geom_point(alpha=0.1)+geom_line(data=tlData,aes(x=x,y=y),color='red')

It seems that ggplot no longer likes have data-less geom components, as the prior version I used was perfectly okay with using aes(x=c(0,100000),y=c(0,100000).

like image 107
Andrew Rohne Avatar answered Oct 11 '22 18:10

Andrew Rohne