错误:ggplot2不知道如何处理不同类的数据 [英] error : ggplot2 doesn't know how to deal with data of class uneval

查看:581
本文介绍了错误:ggplot2不知道如何处理不同类的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

$ g $ p $ g $ g $ p $ g $ =fd,color = id),data = foo1,position =identity,geom =line)+
coord_cartesian(xlim = c(0,200))+
xlab )+
ylab(Density)+
ggtitle(Floods Duration的PDF)+
ggsave(pdf_fd_conus.png)

我写了这个函数

  pdf。 plot <-function(data,x,xl,yl,title,save){
ggplot()+
stat_density(data,kernel =biweight,aes_string(x = x,color ='id '),
position =identity,geom =line)+
coord_cartesian(xlim = c(0,200))+
xlab(xl)+
ylab (yl)+
ggtitle(title)+
ggsave(save)
}

使用此调用:

  pdf.plot(data = foo1,x ='fd',xl =' b',
yl ='a',title ='a',save ='y.png')

但是我得到了th是错误:

 错误:ggplot2不知道如何处理不同类的数据
调用者:eval (expr,envir,enclos)

这是dput(head(foo1,4))

  structure(list(id = structure(c(1L,1L,1L,1L),.Label = c(dfa,$ (b = b,b,b,d,b,c,b,csb,bsk),class =factor),lon = c(-70.978611,
-70.978611,-70.945278,-70.945278) (42.220833,42.220833,
42.190278,42.190278),peakq = c(14.7531,17.3865,3.3414,2.7751
),面积= c(74.3327,74.3327,11.6549,11.6549),fd = c(29 (14.75,35,9.5,
0.5),b1 = c(15485.3,15485.3,b = (0.643551,
0.643551,0.474219,0.474219),k = c(0.325279,0.325279,0.1176624,
0.176624),r = c(81.947,81.947,38.7003,38.7003 ),si = c(0.0037157,
0.0037157,-9999,-9999),rr = c(0.00529193,0.00509193,0.00469513,
0.00469513)),.Names = c(id, lon,lat,peakq,area,
fd,tp,rt,bl,el,k,r ,rr),row.names = c(NA,
4L),class =data.frame)

你能帮忙吗?

解决方案

你的问题是你没有指定什么参数 data 位于 stat_density 中。如果你看看?stat_density ,你会发现第一个暗示的参数实际上是 mapping = 。您需要将 pdf.plot 更改为:

  pdf.plot<函数(data,x,xl,yl,title,save){
ggplot()+
stat_density(data = data,kernel =biweight,aes_string(x = x,color ='id '),
position =identity,geom =line)+
coord_cartesian(xlim = c(0,200))+
xlab(xl)+
ylab (yl)+
ggtitle(title)+
ggsave(save)
}


I have this code

ggplot() + 
  stat_density(kernel = "biweight",aes(x=fd, colour=id), data=foo1,position="identity",geom="line")+
  coord_cartesian(xlim = c(0, 200))+
  xlab("Flood Duration")+
  ylab("Density")+
  ggtitle("PDFs of Flood Duration")+
  ggsave("pdf_fd_conus.png")

And I wrote this function

pdf.plot<-function(data,x,xl,yl,title,save){
  ggplot() + 
    stat_density(data, kernel = "biweight",aes_string(x=x, colour='id'),
                 position="identity",geom="line")+
    coord_cartesian(xlim = c(0, 200))+
    xlab(xl)+
    ylab(yl)+
    ggtitle(title)+
    ggsave(save)
}

Calling using this:

pdf.plot(data=foo1,x='fd',xl='b',
          yl='a',title='a',save='y.png')

But I am getting this error:

Error: ggplot2 doesn't know how to deal with data of class uneval
Called from: eval(expr, envir, enclos)

This is dput(head(foo1,4))

structure(list(id = structure(c(1L, 1L, 1L, 1L), .Label = c("dfa", 
"dfb", "cfa", "csb", "bsk"), class = "factor"), lon = c(-70.978611, 
-70.978611, -70.945278, -70.945278), lat = c(42.220833, 42.220833, 
42.190278, 42.190278), peakq = c(14.7531, 17.3865, 3.3414, 2.7751
), area = c(74.3327, 74.3327, 11.6549, 11.6549), fd = c(29, 54.75, 
23, 1), tp = c(14.25, 19.75, 13.5, 0.5), rt = c(14.75, 35, 9.5, 
0.5), bl = c(15485.3, 15485.3, 8242.64, 8242.64), el = c(0.643551, 
0.643551, 0.474219, 0.474219), k = c(0.325279, 0.325279, 0.176624, 
0.176624), r = c(81.947, 81.947, 38.7003, 38.7003), si = c(0.0037157, 
0.0037157, -9999, -9999), rr = c(0.00529193, 0.00529193, 0.00469513, 
0.00469513)), .Names = c("id", "lon", "lat", "peakq", "area", 
"fd", "tp", "rt", "bl", "el", "k", "r", "si", "rr"), row.names = c(NA, 
4L), class = "data.frame")

Could you please help?

解决方案

Your problem is that you didn't specify what argument data is in stat_density. If you look at ?stat_density you'll see the first implied argument is actually mapping=. You need to change pdf.plot to:

pdf.plot<-function(data,x,xl,yl,title,save){
  ggplot() + 
    stat_density(data = data, kernel = "biweight",aes_string(x=x, colour='id'),
                 position="identity",geom="line")+
    coord_cartesian(xlim = c(0, 200))+
    xlab(xl)+
    ylab(yl)+
    ggtitle(title)+
    ggsave(save)
}

这篇关于错误:ggplot2不知道如何处理不同类的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆