【问题标题】:combining multicolumn and multiple geom legend结合多列和多个几何图例
【发布时间】:2021-02-23 10:03:24
【问题描述】:

我正在尝试将颜色和线型图例合二为一,同时还有一个包含两列的图例。出于某种原因,它失败了,尽管我尝试了标准方法,具有相同的 - 缺少 - 图例名称。我很感激任何提示!

这是数据的子集:

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"))

这是情节的代码:

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())

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    linetypecolor 包括类似的labels

    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())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      相关资源
      最近更新 更多