【问题标题】:Adding legend for combo bar and line graph -- ggplot ignoring commands为组合条和折线图添加图例——ggplot 忽略命令
【发布时间】:2019-06-13 09:35:58
【问题描述】:

我也在尝试制作带有折线图的条形图。该图创建得很好,但图例不想将线图添加到图例中。

我尝试了很多不同的方法将这些添加到图例中,包括:

ggplot Legend Bar and Line in Same Graph

这些都没有奏效。 show.legendgeom_line aes 中似乎也被忽略了。

我创建图表的代码如下:

ggplot(first_q, aes(fill = Segments)) +
 geom_bar(aes(x= Segments, y= number_of_new_customers), stat = 
      "identity") + theme(axis.text.x = element_blank()) +
  scale_y_continuous(expand = c(0, 0), limits = c(0,3000)) + 
  ylab('Number of Customers') + xlab('Segments') +
  ggtitle('Number Customers in Q1 by Segments') +theme(plot.title = 
       element_text(hjust = 0.5)) +
  geom_line(aes(x= Segments, y=count) ,stat="identity", 
     group = 1, size = 1.5, colour = "darkred", alpha = 0.9, show.legend = 
     TRUE) +
  geom_line(aes(x= Segments, y=bond_count) 
       ,stat="identity", group = 1, size = 1.5, colour = "blue", alpha = 
        0.9)  +
  geom_line(aes(x= Segments, y=variable_count) 
       ,stat="identity", group = 1, size = 1.5, colour = "darkgreen", 
     alpha = 0.9) +
  geom_line(aes(x= Segments, y=children_count) 
        ,stat="identity", group = 1, size = 1.5, colour = "orange", alpha 
         = 0.9) +
  guides(fill=guide_legend(title="Segments")) + 
  scale_color_discrete(name = "Prod", labels = c("count", "bond_count", "variable_count", "children_count)))

我对 R 相当陌生,所以如果需要任何进一步的信息,或者如果这个问题可以更好地表达,请告诉我。

非常感谢任何帮助。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    好的,你需要删除一些你的东西。我使用了 mtcars 数据集,因为您没有提供您的数据集。我试图保留你的变量名并将情节减少到必要的部分。代码如下:

    first_q <- mtcars
    first_q$Segments <- mtcars$mpg
    first_q$val <- seq(1,nrow(mtcars))
    first_q$number_of_new_costumers <- mtcars$hp
    first_q$type <- "Line"
    
    ggplot(first_q) +
      geom_bar(aes(x= Segments, y= number_of_new_costumers, fill = "Bar"), stat = 
                 "identity") + theme(axis.text.x = element_blank()) +
      scale_y_continuous(expand = c(0, 0), limits = c(0,3000)) +
      geom_line(aes(x=Segments,y=val, linetype="Line"))+
      geom_line(aes(x=Segments,y=disp, linetype="next line"))
    

    您链接的答案已经给出了答案,但我尝试解释。您想通过使用数据的不同属性来绘制图例。所以如果你想使用不同的行,你可以在aes 中声明。这就是您的图例中显示的内容。所以我在这里使用了两个不同的geom_lines。因为 aes 都是 linetype,所以都显示在图例 linetype

    剧情:

    您可以轻松地调整它以适应您的使用。如果您想以这种方式解决它,请确保使用已知的美学关键字。您也可以在之后使用以下方法更改标题名称:

    labs(fill = "costum name")
    

    如果您想添加颜色和相同的线型,您可以使用scale_linetype_manual 进行自定义,如下所示(我这次没有使用填充):

    library(ggplot2)
    first_q <- mtcars
    first_q$Segments <- mtcars$mpg
    first_q$val <- seq(1,nrow(mtcars))
    first_q$number_of_new_costumers <- mtcars$hp
    first_q$type <- "Line"
    cols = c("red", "green")
    ggplot(first_q) +
      geom_bar(aes(x= Segments, y= number_of_new_costumers), stat = 
                 "identity") + theme(axis.text.x = element_blank()) +
      scale_y_continuous(expand = c(0, 0), limits = c(0,3000)) +
      geom_line(aes(x=Segments,y=val, linetype="solid"),  color = "red", alpha = 0.4)+
      geom_line(aes(x=Segments,y=disp, linetype="second"), color ="green", alpha = 0.5)+
      scale_linetype_manual(values = c("solid","solid"),
                          guide = guide_legend(override.aes = list(colour = cols)))  
    

    【讨论】:

    • 几乎成功了!唯一的问题是,当更改 aes 之外的线的属性时,图例会将我的线作为一个空框。 geom_line(aes(x= Segments, y=savings_count, linetype="Savings"), stat="identity", group = 1, size = 1.5, colour = "darkred", alpha = 0.9) 这是将 Savings 放入图例中,尽管是一个空框。很抱歉无法共享数据,这是保密的。
    • @geds133 将 alpha 放入 aes() 并将 scale_alpha(guide="none") 添加到您的 ggplot 中。欲了解更多信息,请查看this答案
    • 完美运行。非常感谢
    • 图例中的所有线条颜色都是相同的颜色。有没有办法将线型颜色更改为图表中的相应颜色。它们也变得星罗棋布。我希望它们是一条实线。
    猜你喜欢
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2023-01-27
    • 2013-06-29
    • 1970-01-01
    相关资源
    最近更新 更多