折叠ggplot2传说

时间:2015-08-24 20:38:43

标签: r ggplot2

编辑: 我花了很多时间打字,但得到反馈我的问题不清楚,所以我正在编辑以试图澄清。

获取以下代码并运行它。请注意,第三个图有两个图例。我想让第三个情节看起来像第二个情节,而不必像我做的那样硬编码来创建第二个情节。

原帖: 我试图显示有关多家公司及其产品的数据。

基本图表(图表1)有点难以理解,特别是一旦我添加了几家公司。

所以,我正在努力推动公司的颜色和线型产品。

我可以手动控制它(图2),但这似乎是一个糟糕的解决方案。

我可以自动完成(图3),但有几个问题: - 我不再确定如何再次完全手动控制颜色 - 我有两个传说

我搜索并在网上看到你可以通过标题和名称相同来删除传说。但这需要手动命名所有内容,我正在努力避免,因为如果新公司或产品出现,图表将不再有效。

有关如何动态处理此问题的任何建议都将非常感激。 顺便说一句,我很想知道ggplot2在手动分配时如何处理标签或颜色。似乎没有什么可以将数据与颜色或名称的顺序联系起来,所以我不得不相信,如果您重新订购数据或获得新数据,您的名字可能不正确?

# libraries
library(ggplot2)
library(dplyr)

marketShareDF <- structure(list(Company = c("Company1", "Company1", "Company1", 
                                         "Company1", "Company1", "Company1", "Company1", "Company1", "Company1", 
                                         "Company1", "Company1", "Company1", "Company2", "Company2", "Company2", 
                                         "Company2", "Company2", "Company2", "Company2", "Company2", "Company2", 
                                         "Company3", "Company3", "Company3", "Company3", "Company3", "Company3", 
                                         "Company3", "Company3"), 
                             Product = c("ProductA", "ProductA", 
                                         "ProductA", "ProductA", "ProductA", "ProductA", "ProductB", "ProductB", 
                                         "ProductB", "ProductB", "ProductB", "ProductB", "ProductA", "ProductA", 
                                         "ProductA", "ProductA", "ProductA", "ProductA", "ProductB", "ProductB", 
                                         "ProductB", "ProductA", "ProductA", "ProductB", "ProductB", "ProductB", 
                                         "ProductB", "ProductB", "ProductB"), 
                             Combined = c("Co1ProductA", 
                                          "Co1ProductA", "Co1ProductA", "Co1ProductA", "Co1ProductA", "Co1ProductA", 
                                          "Co1ProductB", "Co1ProductB", "Co1ProductB", "Co1ProductB", "Co1ProductB", 
                                          "Co1ProductB", "Co2ProductA", "Co2ProductA", "Co2ProductA", "Co2ProductA", 
                                          "Co2ProductA", "Co2ProductA", "Co2ProductB", "Co2ProductB", "Co2ProductB", 
                                          "Co3ProductA", "Co3ProductA", "Co3ProductB", "Co3ProductB", "Co3ProductB", 
                                          "Co3ProductB", "Co3ProductB", "Co3ProductB"), 
                             Year = c(2010, 
                                      2011, 2012, 2013, 2014, 2015, 2010, 2011, 2012, 2013, 2014, 2015, 
                                      2010, 2011, 2012, 2013, 2014, 2015, 2013, 2014, 2015, 2010, 2011, 
                                      2010, 2011, 2012, 2013, 2014, 2015), 
                             PercentOfMarket = c(0.08, 
                                                 0.046, 0.035, 0.032, 0.018, 0.007, 0.163, 0.142, 0.127, 0.111, 
                                                 0.163, 0.112, 0.084, 0.083, 0.079, 0.064, 0.043, 0.006, 0.011, 
                                                 0.546, 0.413, 0.088, 0.294, 0.129, 0.141, 0.17, 0.185, 0.166, 
                                                 0.073), 
                             c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                               NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                               NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                               NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                               NA_real_, NA_real_)), 
                        class = c("tbl_df", "tbl", "data.frame"), 
                        row.names = c(NA, -29L), 
                        .Names = c("Company", "Product", "Combined", "Year", "PercentOfMarket", ""))

#Basic chart
marketShareDF %>% ggplot() +
  geom_line(aes(Year, PercentOfMarket, group=Combined, color=Combined, linetype=Combined))

# Color by company, lines by Product - manual
marketShareDF %>% ggplot() +
  geom_line(aes(Year, PercentOfMarket, group=Combined, color=Combined, linetype=Combined)) +
  scale_linetype_manual(values=c(1,2,1,2,1,2)) +
  scale_color_manual(values=c("red", "red", "blue", "blue", "green", "green"))

# Color by company, lines by Product - automatic
marketShareDF %>% ggplot() +
  geom_line(aes(Year, PercentOfMarket, group=Combined, color=Company, linetype=Product)) 

1 个答案:

答案 0 :(得分:0)

可以使用吗?

library(RColorBrewer)
marketShareDF %>% ggplot() +
        geom_line(lwd = 1,aes(Year, PercentOfMarket,group=Combined, color=Combined, linetype=Combined)) +
        scale_linetype_manual(values = c(rep("solid", 3), rep("dashed", 3))) +
        scale_color_manual(values = c(brewer.pal(3, "Set3"),brewer.pal(3, "Set3"))) 

enter image description here

改编自Controlling line color and line type in ggplot legend