Search code examples
rggplot2facet-wrap

Using different types lynes using ggplot with facet_wrap


Let's say that my data has the following structure

structure(list(MODEL = c("SHAR", "SHAR", "SHAR", "SHAR", "SHAR", 
"SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "SHAR", "MHAR", 
"MHAR", "MHAR", "MHAR", "MHAR", "MHAR", "MHAR", "MHAR", "MHAR", 
"MHAR", "MHAR", "MHAR"), name = c("NSW", "QLD", "SA", "VIC", 
"NSW", "QLD", "SA", "VIC", "NSW", "QLD", "SA", "VIC", "NSW",     
"QLD", "SA", "VIC", "NSW", "QLD", "SA", "VIC", "NSW", "QLD", 
"SA", "VIC"), value = c(-0.429298062117084, 0.369924963217068, 
-0.337914242584103, 0.352630004951941, -2.08652175501946, -0.788308699403464, 
-0.283024876840677, -0.673127168726321, -1.68696080065691, -0.584489188226214, 
0.648107971996921, -0.861309510260168, -1.63384189007935, 0.718255492983859, 
-1.17198565174371, 0.591123365147864, 0.208861829604905, -0.596711833086073, 
-1.33166531555532, -1.31370104749052, -1.04879351668995, -0.333940489158404,     
1.40014661442373, 0.270226863552986), Date = structure(c(2011, 
2011, 2011, 2011, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 2013, 
2011, 2011, 2011, 2011, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 
2013), class = "Date")), class = "data.frame", row.names = c(NA, 
-24L))

I am using facet_wrap for plotting the results of two models and I want that each model has a different line type. I tried using ´scale_linetype_manual´ but I believe that this is not the appropriate command with facet_wrap. The code that I am using is the following :

library(ggplot2)
ggplot(aes(x = as.Date(Date), y=value, colour = MODEL), data = EXAMPLE) +
  scale_color_manual(values = c("skyblue3", "firebrick3"), 
                     labels = c("MHAR-ReCov Model", 
                                "SHAR Model")) +
  
  scale_linetype_manual(values = c(2,5), 
                        labels = c("MHAR-ReCov Model", 
                                   "SHAR Model")) +
  geom_line() + facet_wrap(vars(name), ncol = 4, scales = "free")+
  theme_test() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1), 
        legend.position = "bottom",
        legend.title = element_blank())+
  labs(x = "", y="")

I will grateful if someone can help me.


Solution

  • Include linetype = MODEL in aes :

    library(ggplot2)
    
    ggplot(aes(x = as.Date(Date), y=value, colour = MODEL, 
               linetype = MODEL), data = EXAMPLE) +
      scale_color_manual(values = c("skyblue3", "firebrick3"), 
                         labels = c("MHAR-ReCov Model", 
                                    "SHAR Model")) +
      scale_linetype_manual(values = c(2,5), 
                            labels = c("MHAR-ReCov Model", 
                                       "SHAR Model")) +
      geom_line() + facet_wrap(vars(name), ncol = 4, scales = "free")+
      theme_test() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1), 
            legend.position = "bottom",
            legend.title = element_blank())+
      labs(x = "", y="")
    

    enter image description here