4

我是 R 新手,不知道如何用 ggplot2 绘制两个 data.frames。我收到以下错误消息:Error: ggplot2 doesn't know how to deal with data of class uneval

如何将我的数据与底层世界地图放在一起?

这是我的代码:

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)
4

1 回答 1

5

如果使用多个数据集,请尝试从函数中提取dataaes信息,并根据需要ggplot 将其放入每个geom_*对象中。

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)

关于论点

请注意,虽然函数的第一个参数ggplot(.)是,但对于大多数(任何?) sdata而言,情况并非如此。geom_*他们的第一个论点是mapping.
因此,如果您使用的第一个参数是数据集,请确保明确命名,
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
于 2013-05-12T20:39:58.947 回答