Home > database >  Can't change line type for geom_path in ggplot2
Can't change line type for geom_path in ggplot2

Time:07-01

I have a data set (a small excerpt below) that is plotting three lines - two estimated from one model with confidence intervals and a third line that serves as an external confirmation of the results.

bestFit <- data.frame(Year = c(2006, 2007, 2008,
                               2006, 2007, 2008,
                               2006, 2007, 2008),
                      Estimate = c(4208, 4570, 4925,
                                   0872, 1324, 1193,
                                   6009, 5705, 6474),
                      Lo95CI = c(2903, 3561, 4036,
                                 0693, 1113, 0999,
                                   NA,   NA,   NA),
                      Hi95CI = c(5209, 5453, 5725,
                                 0998, 1489, 1330,
                                   NA,   NA,   NA),
                      Reconstruction = c("Abun", "Abun", "Abun",
                                         "Recr", "Recr", "Recr",
                                         "Extl", "Extl", "Extl"))
bestFit$Reconstruction <- factor(bestFit$Reconstruction, ordered = TRUE,
                         levels = c("Abun", "Recr", "Extl"))

ggplot(aes(y = Estimate, x = Year, fill = Reconstruction), data = bestFit)   
    geom_ribbon(aes(ymin = Lo95CI, ymax = Hi95CI, 
                    fill = Reconstruction), alpha = 0.25)  
    geom_point(aes(colour = Reconstruction))   
    geom_path(aes(colour = Reconstruction), size = 1)  
    scale_x_continuous(breaks = seq(2006, 2008, 2))  
    scale_fill_manual(values = c("#F8766D", "#00BA38", "#619CFF"))   
    scale_color_manual(values = c("#F8766D", "#00BA38", "#619CFF"))  
    scale_linetype_manual(values = c("solid", "solid", "dotted"))

enter image description here

I can manually set the colors, but am having trouble manually setting the line type. I want the first two lines (which are from my model) to be solid and the third line (which is from an external model) dashed.

CodePudding user response:

From comments:

You haven't mapped the linetype aesthetic. Add linetype = Reconstruction inside the aes portion of your geom_path call.

  • Related