【问题标题】:ggplot2 doesn't recognise values from molten dataggplot2 无法识别熔融数据中的值
【发布时间】:2016-05-06 22:47:27
【问题描述】:

我正在尝试按照此处的示例制作一个简单的堆叠密度图:Create a stacked density graph in ggplot2。

我正在使用融化的数据,但是当我尝试在 y 轴上用“值”绘制它时,我得到了一个错误。

这是我的原始数据:

> str(bug)
'data.frame':   39 obs. of  10 variables:
 $ year           : int  2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 ...
 $ SampleDate     : Factor w/ 39 levels "01-Aug-07","02-Jun-06",..: 21 32 2 11 19 28 37 8 17 24 ...
 $ Oligochaeta    : int  905 604 371 991 213 144 1 14 5 5 ...
 $ Copepoda       : int  586 352 12 602 45 24 3 1 1 8 ...
 etc. 

这就是我使用融化的方式:

> mbug <- melt(bug, id.vars=c("year", "SampleDate"))

我更正了日期格式:

> mbug$date1 <- as.Date(mbug$SampleDate, "%d-%b-%y")

这是熔化的数据:

> str(mbug)
'data.frame':   312 obs. of  5 variables:
 $ year      : int  2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 ...
 $ SampleDate: Factor w/ 39 levels "01-Aug-07","02-Jun-06",..: 21 32 2 11 19 28 37 8 17 24 ...
 $ variable  : Factor w/ 8 levels "Oligochaeta",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value     : int  905 604 371 991 213 144 1 14 5 5 ...
 $ date1     : Date, format: "2006-05-18" "2006-05-26" "2006-06-02" "2006-06-09" ...

我使用 ggplot 处理熔融数据:

> ggplot(mbug, aes(x=date1, y=value)) +
+ geom_density(aes(fill=variable), position="stack")
+ facet_wrap(~ year, nrow=1) + theme(text = element_text(size = 22))

最后出现了这个错误:

Error in eval(expr, envir, enclos) : object 'y' not found

最终,我希望将变量称为“分类单元”并将值称为“丰度”,但我以后总是可以在 ggplot 中做到这一点。现在,我的主要目标是能够绘制 y!

提前致谢。

【问题讨论】:

  • 将aes(x=date1, y=value) 更改为aes(value)(或aes(x=value))。密度图是给出单个变量分布的平滑直方图。密度绘制在 y 轴上,由 ggplot 在内部计算。
  • 使用内置数据框复制此错误:ggplot(mtcars, aes(x=wt, y=mpg, fill=factor(cyl))) + geom_density()。要获得所需的情节:ggplot(mtcars, aes(x=mpg, fill=factor(cyl))) + geom_density().

标签: r plot ggplot2 reshape2


【解决方案1】:

看起来您想要的可能不是密度图,而是value 与date1 的面积图。可能是这样的,但如果不能使用您的数据样本就很难确定:

ggplot(mbug, aes(x=date1, y=value, fill=variable)) +
    geom_area() +
    facet_wrap(~ year, nrow=1) + 
    theme(text = element_text(size = 22))

为了比较,这里有一个假数据的例子:

set.seed(984)
dat = data.frame(time=rep(1980:2020,2), 
                 value=c(cumsum(rnorm(41,10,20)),cumsum(rnorm(41,10,20))),
                 group=rep(c("A","B"), each=41))

ggplot(dat, aes(time, value, fill=group)) + 
  geom_area() +
  theme_bw()

密度图是给出单个变量分布的平滑直方图。密度绘制在 y 轴上,由 ggplot 在内部计算。例如,如果我想要车辆燃油经济性的分布(即每个燃油经济性区间内汽车的比例),我可以这样做:

ggplot(mtcars, aes(mpg)) + geom_density()

但如果我执行以下操作,则会导致您遇到同样的错误:

ggplot(mtcars, aes(x=wt, y=mpg) + geom_density()

这是因为密度图中的“y”值是 x 变量在每个 x 值处的密度(y 轴上的高度)。

【讨论】:

  • 谢谢!是的,这已经奏效,只需再进行一次调整即可使其完美。使用facet_wrap,我每年都有一个面板。但在该面板中,它绘制了整个日期范围。我只想要每年的五月到八月。这是我更正日期格式的部分原因......使用geom_line,我能够定义group=year。 geom_area 中是否有等价物?
  • 没关系,我解决了:ggplot(mbug, aes(x=date1, y=value, fill=variable)) + geom_area(stat="identity") + facet_wrap(~ year, nrow=1, scales="free_x") + theme(text = element_text(size = 22)) 再次感谢您简洁明了的回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 2022-01-14
相关资源
最近更新 更多