Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading