ggplot2 two data.frames, doesn't know how to deal with data of class uneval

Til Hund picture Til Hund · May 12, 2013 · Viewed 18.9k times · Source

I'm new to R and do not know how to plot two data.frames with ggplot2. I get the following error message: Error: ggplot2 doesn't know how to deal with data of class uneval

How can I put together my data with the underlying world map?

Here is my code:

require(Hmisc)
require(mapproj)
require(ggplot2)
require(rgdal)
require(maptools)
require(sp)
require(cshapes)
gpclibPermit()

world <- cshp(date=as.Date("2008-1-1"))
world.points <- fortify(world, region='COWCODE')
p <- ggplot(world.points, aes(long,lat,group=group)) + geom_polygon()

dat <- mdb.get("CLIWOC15_2000.mdb") # you can get the data from here: http://pendientedemigracion.ucm.es/info/cliwoc/cliwoc15.htm

tmp <- dat$CLIWOC15[,c("Lon3","Lat3")]

ggplot(world.points,aes(long,lat,group=group))
+geom_polygon()
+geom_point()
+geom_histogram(tmp,aes(Lon3,Lat3),alpha=0.01,size=1)
+coord_map()+ylim(-90,90)

Answer

Ricardo Saporta picture Ricardo Saporta · May 12, 2013

If using multiple datasets, try pulling the data and aes info out of the ggplot function and putting it instead in each of the geom_* objects as needed.

ggplot() +
  geom_polygon(data=world.points,aes(long,lat,group=group)) +
  geom_point(data=world.points,aes(long,lat,group=group)) +

  # Separately, I'm not sure what the intended outcome is for this histogram, but it doesn't appear to be of a correct form 
  geom_histogram(data=tmp,aes(Lon3,Lat3),alpha=0.01,size=1) +
  coord_map() + 
  ylim(-90,90)

with regards to the arguments

Notice that while the first argument to the function ggplot(.) is data, this is not the case for most (any?) of the geom_*s. Their first argument is the mapping.
Hence if the first argument you are using is a dataset, make sure to name it explicity,
ie geom_point(data=myDataFrame, .)

# You can always check the arguments by using the `args(.)` function

> args(ggplot)
function (data = NULL, ...) 
NULL

> args(geom_polygon)
function (mapping = NULL, data = NULL, stat = "identity", position = "identity", ...) 
NULL

> args(geom_histogram)
function (mapping = NULL, data = NULL, stat = "bin", position = "stack", ...) 
NULL