combining multicolumn and multiple geom legend

Viewed 37

I am trying to combine the color and the linetype legends into one, while also having a legend with two columns. For some reason it fails, although I tried the standard approach, having the same - missing - legend name. I appreciate any hints!

This is a subset of the data:

to_plot <- structure(list(year = structure(c(2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 
2017, 2017), labels = c(`1977` = 1977, `1978` = 1978, `1979` = 1979, 
`1980` = 1980, `1981` = 1981, `1982` = 1982, `1983` = 1983, `1984` = 1984, 
`1985` = 1985, `1986` = 1986, `1987` = 1987, `1988` = 1988, `1989` = 1989, 
`1990` = 1990, `1991` = 1991, `1992` = 1992, `1993` = 1993, `1994` = 1994, 
`1995` = 1995, `1996` = 1996, `1997` = 1997, `1998` = 1998, `1999` = 1999, 
`2000` = 2000, `2001` = 2001, `2002` = 2002, `2003` = 2003, `2004` = 2004, 
`2005` = 2005, `2006` = 2006, `2007` = 2007, `2008` = 2008, `2009` = 2009, 
`2010` = 2010, `2011` = 2011, `2012` = 2012, `2013` = 2013, `2014` = 2014, 
`2015` = 2015, `2016` = 2016, `2017` = 2017, `2018` = 2018), label = "Erhebungsjahr", class = c("haven_labelled", 
"vctrs_vctr", "double")), month = structure(c(1, 2, 3, 4, 5, 
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 1, 2, 2, 3, 
3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10), labels = c(Januar = 1, 
Februar = 2, März = 3, April = 4, Mai = 5, Juni = 6, Juli = 7, 
August = 8, September = 9, Oktober = 10, November = 11, Dezember = 12
), label = "Erhebungsmonat", class = c("haven_labelled", "vctrs_vctr", 
"double")), Pegida = c(16.1120511374318, 14.8082942097027, 14.4972239358421, 
13.6968334270189, 10.6391347099312, 11.4613738081339, 11.703056768559, 
12.4033954136577, 10.9765509053131, 11.10442974544, 3.30788804071247, 
2.52822778595974, 2.2292158405455, 2.27272727272727, 2.22544750846638, 
2.24079457364341, 2.32160312805474, 2.6168070001651, 2.50919793966152, 
2.9578278451762, 1.01449275362319, 0.579710144927536, 1.01449275362319, 
0.289855072463768, 1.01449275362319, 0.434782608695652, 1.01449275362319, 
0.579710144927536, 1.15942028985507, 0.579710144927536, 0.289855072463768, 
0.144927536231884, 0.869565217391304, 0.144927536231884, 0.72463768115942, 
0, 1.88405797101449, 0.869565217391304, 0.869565217391304, 0.434782608695652
), type = c("Issue salience in the public", "Issue salience in the public", 
"Issue salience in the public", "Issue salience in the public", 
"Issue salience in the public", "Issue salience in the public", 
"Issue salience in the public", "Issue salience in the public", 
"Issue salience in the public", "Issue salience in the public", 
"Popularity of AfD", "Popularity of AfD", "Popularity of AfD", 
"Popularity of AfD", "Popularity of AfD", "Popularity of AfD", 
"Popularity of AfD", "Popularity of AfD", "Popularity of AfD", 
"Popularity of AfD", "Issue salience in the press", "Co-occurance of issue and AfD in the press", 
"Issue salience in the press", "Co-occurance of issue and AfD in the press", 
"Issue salience in the press", "Co-occurance of issue and AfD in the press", 
"Issue salience in the press", "Co-occurance of issue and AfD in the press", 
"Issue salience in the press", "Co-occurance of issue and AfD in the press", 
"Issue salience in the press", "Co-occurance of issue and AfD in the press", 
"Issue salience in the press", "Co-occurance of issue and AfD in the press", 
"Issue salience in the press", "Co-occurance of issue and AfD in the press", 
"Issue salience in the press", "Co-occurance of issue and AfD in the press", 
"Issue salience in the press", "Co-occurance of issue and AfD in the press"
), date = structure(c(17167, 17198, 17226, 17257, 17287, 17318, 
17348, 17379, 17410, 17440, 17167, 17198, 17226, 17257, 17287, 
17318, 17348, 17379, 17410, 17440, 17167, 17167, 17198, 17198, 
17226, 17226, 17257, 17257, 17287, 17287, 17318, 17318, 17348, 
17348, 17379, 17379, 17410, 17410, 17440, 17440), class = "Date")), row.names = c(NA, 
-40L), class = c("tbl_df", "tbl", "data.frame"))

This is the code for the plot:

ggplot(to_plot, aes(x=date, y=Pegida*3, linetype=type, color=type)) +
  geom_line() +
  scale_linetype_manual(name="", breaks = c("Protest Events", "Issue salience in the public",
                                            "Popularity of AfD", "Issue salience in the press",
                                            "Co-occurance of issue and AfD in the press"),
                        values = c("solid", "dashed", "dotted", "twodash", "12345678")) + 
  scale_color_manual(name="", values = c("#0280B5", "#A13375", "#619933", "#FF0000", "#000000")) +
  ylab("Share (percentage point)") +
  guides(linetype=guide_legend(ncol=2),
         color=guide_legend(ncol=2)) +
  theme(legend.title=element_blank(), legend.position="bottom",
        legend.key.width = unit(2,"cm"), axis.title.x = element_blank())
1 Answers

Include similar labels for linetype and color.

library(ggplot2)

ggplot(to_plot, aes(x=date, y=Pegida*3, linetype=type, color=type)) +
  geom_line() +
  scale_linetype_manual(name="", labels = c("Protest Events", "Issue salience in the public",
                                            "Popularity of AfD", "Issue salience in the press",
                                            "Co-occurance of issue and AfD in the press"),
                        values = c("solid", "dashed", "dotted", "twodash", "12345678")) + 
  scale_color_manual(name="", values = c("#0280B5", "#A13375", "#619933", "#FF0000", "#000000"), 
                     labels = c("Protest Events", "Issue salience in the public",
                                "Popularity of AfD", "Issue salience in the press",
                                "Co-occurance of issue and AfD in the press")) +
  ylab("Share (percentage point)") +
  guides(linetype=guide_legend(ncol=2),
         color=guide_legend(ncol=2)) +
  theme(legend.title=element_blank(), legend.position="bottom",
        legend.key.width = unit(2,"cm"), axis.title.x = element_blank())

enter image description here

Related