简体   繁体   中英

How to correctly specify a column as the fill colour in geom_ribbon?

I can't seem to be able to set different fill colours for geom_ribbon() , using one of the columns as input to fill

library(ggplot2)
time <- as.factor(c('A','B','C','D'))
grouping <- as.factor(c('GROUP1','GROUP1','GROUP1','GROUP1',
                        'GROUP2','GROUP2','GROUP2','GROUP2'))
x <- c(1.00,1.03,1.03,1.06,0.5,0.43,0.2,0.1)
x.upper <- x+0.05
x.lower <- x-0.05

df <- data.frame(time, x, x.upper, x.lower,grouping)


ggplot(data = df,aes(as.numeric(time),x,group=grouping,color=grouping)) +
  geom_ribbon(data = df, aes(x=as.numeric(time), ymax=x.upper, ymin=x.lower),     
              fill=grouping, alpha=.5) +
  geom_point() + labs(title="My ribbon plot",x="Time",y="Value") +
  scale_x_continuous(breaks = 1:4, labels = levels(df$time))

I get the error Error: Unknown colour name: grouping but fill=c("pink","blue") works fine. I don't want to specify the colours manually.

All other examples I can find simply list the column in the fill argument so I'm not sure what I'm doing incorrectly.

Move fill = grouping inside aes so that this column is mapped to the fill variable.

ggplot(data = df, aes(as.numeric(time), x, color = grouping)) +
  geom_ribbon(data = df, aes(ymax = x.upper, ymin = x.lower,     
              fill = grouping), alpha = 0.5) +
  geom_point() + 
  labs(title = "My ribbon plot", x = "Time", y = "Value") +
  scale_x_continuous(breaks = 1:4, labels = levels(df$time))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM