5

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

lebelinoz
  • 4,890
  • 10
  • 33
  • 56
LucaS
  • 887
  • 1
  • 9
  • 22
  • 1
    You will want to add a column in your data to deal with the different colors: https://stackoverflow.com/questions/19167944/ggplot2-use-different-colors-in-different-facets. You might be able to get there with a vector of colors as well. I can't test anything on your example. – Shawn Mehan Jun 14 '17 at 06:31
  • 1
    Can't download the data. Require password. – Adam Quek Jun 14 '17 at 06:32
  • 1
    You probably just need `aes(colour = Model, group = Model)` inside your `geom_line`, and Model should be converted to a factor first. – neilfws Jun 14 '17 at 06:46

1 Answers1

2

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)
neilfws
  • 32,751
  • 5
  • 50
  • 63
  • Thank you. That works pretty well - but how would I change the fill colour of the ribbon? I've worked out how to change the line colour by adding: `scale_color_manual(values=c("black", "blue", "red", "green"))` and I'd like to match the ribbon fill colour to the line colour. – LucaS Jun 14 '17 at 08:40