ggplot2,填充线下区域时出错

Geo*_*-sp 0 plot r ggplot2

我有此数据集,我想填写area under each line。但是我收到一条错误消息:

错误:stat_bin()不得用于美观。

另外,我需要使用alpha透明性值。有什么建议么?

library(reshape2)
library(ggplot2)

dat <- data.frame(
    a = rnorm(12, mean = 2, sd = 1),
    b = rnorm(12, mean = 4, sd = 2),
    month = c("JAN","FEB","MAR",'APR',"MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"))

dat$month <- factor(dat$month, 
    levels = c("JAN","FEB","MAR",'APR',"MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"), 
    ordered = TRUE)

dat <- melt(dat, id="month")

ggplot(data = dat, aes(x = month, y = value, colour = variable)) + 
    geom_line() + 
    geom_area(stat ="bin")
Run Code Online (Sandbox Code Playgroud)

Gre*_*gor 5

我要填满每行下的区域

这意味着我们将需要指定fill美学。

我说错了 "Error: stat_bin() must not be used with a y aesthetic."

这意味着我们将需要删除您的stat ="bin"代码。

此外,我需要使用透明度的alpha值。

这意味着我们需要放入alpha = <some value>geom_area层。

另外两件事:(1)由于您在x轴上有一个因子,因此我们需要指定一个分组,以便ggplot知道要连接的点。在这种情况下,我们可以variable用作石斑鱼。(2)的默认“位置” geom_area是堆叠区域而不是重叠区域。因为您询问透明度,所以我假设您希望它们重叠,因此我们需要指定position = 'identity'

ggplot(data = dat, aes(x = month, y = value, colour = variable)) +
    geom_line() +
    geom_area(aes(fill = variable, group = variable),
              alpha = 0.5, position = 'identity')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明