Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vary line and ribbon colours in a facet_grid

Tags:

r

ggplot2

I'm hoping someone can help with this plotting problem I have. The data can be found here.

Basically I want to plot a line (mean) and it's associated confidence interval (lower, upper) for 4 models I have tested. I want to facet on the Cat_Auth variable for which there are 4 categories (so 4 plots). The first 'model' is actually just the mean of the sample data and I don't want a CI for this (NA values specified in the data - not sure if this is the correct thing to do).

I can get the plot some way there with:

newdata <- read.csv("data.csv", header=T)
ggplot(newdata, aes(x = Affil_Max, y = Mean)) + 
  geom_line(data = newdata, aes(), colour = "blue") +
  geom_ribbon(data = newdata, alpha = .5, aes(ymin = Lower, ymax = Upper, group = Model, fill = Model)) +
  facet_grid(.~ Cat_Auth)

But I'd like different coloured lines and shaded ribbons for each model (e.g. a red mean line and red shaded ribbon for model 2, green for model 3 etc). Also, I can't figure out why the blue line corresponding to the first set of mean values is disjointed as it is.

Would be really grateful for any assistance!

Plot

like image 629
LucaS Avatar asked Oct 29 '22 06:10

LucaS


1 Answers

Try this:

library(dplyr)
library(ggplot2)

newdata %>%
  mutate(Model = as.factor(Model)) %>%
  ggplot(aes(Affil_Max, Mean)) + 
    geom_line(aes(color = Model, group = Model)) +
    geom_ribbon(alpha = .5, aes(ymin = Lower, ymax = Upper, 
                                group = Model, fill = Model)) +
    facet_grid(. ~ Cat_Auth)
like image 196
neilfws Avatar answered Nov 15 '22 07:11

neilfws