error : ggplot2 doesn't know how to deal with data of class uneval

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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?

回答1:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!