简体   繁体   English

为组合栏和折线图添加图例 - ggplot忽略命令

[英]Adding legend for combo bar and line graph — ggplot ignoring commands

I am trying to make a bar chart with line plots as well. 我正在尝试制作带有线图的条形图。 The graph has created fine but the legend does not want to add the line plots to the legend. 图表已创建正常,但图例不希望将线图添加到图例中。

I have tried so many different ways of adding these to the legend including: 我尝试了很多不同的方法将这些添加到图例中,包括:

ggplot Legend Bar and Line in Same Graph ggplot图例栏和相同图表中的线

None of which have worked. 其中没有一个有效。 show.legend also seems to have been ignored in the geom_line aes. show.legend也似乎在被忽略geom_line AES。

My code to create the graph is as follows: 我创建图表的代码如下:

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

I am fairly new to R so if any further information is required or if this question could be better represented then please let me know. 我是R的新手,所以如果需要任何进一步的信息或者这个问题可以更好地代表,请告诉我。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Alright, you need to remove a little bit of your stuff. 好吧,你需要删除一些你的东西。 I used the mtcars dataset, since you did not provide yours. 我使用了mtcars数据集,因为你没有提供你的数据集。 I tried to keep your variable names and reduced the plot to necessary parts. 我试图保留你的变量名称并将绘图缩小到必要的部分。 The code is as follows: 代码如下:

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

The answer you linked already gave the answer, but i try to explain. 你联系的答案已经给出了答案,但我试着解释一下。 You want to plot the legend by using different properties of your data. 您希望使用数据的不同属性来绘制图例。 So if you want to use different lines, you can declare this in your aes . 所以,如果你想使用不同的线路,您可以在此声明aes This is what get's shown in your legend. 这就是你的传奇中所展示的内容。 So i used two different geom_lines here. 所以我在这里使用了两个不同的geom_lines Since the aes is both linetype , both get shown at the legend linetype . 由于aes都是linetype ,因此都会在图例linetype

the plot: 剧情: 输出图

You can adapt this easily to your use. 您可以根据自己的需要轻松调整。 Make sure you using known keywords for the aesthetic if you want to solve it this way. 如果您想以这种方式解决问题,请确保使用已知关键字进行审美。 Also you can change the title names afterwards by using: 您也可以使用以下方法更改标题名称:

labs(fill = "costum name")

If you want to add colours and the same line types, you can do customizing by using scale_linetype_manual like follows (i did not use fill for the bars this time): 如果你想添加颜色和相同的线条类型,你可以使用scale_linetype_manual进行自定义,如下所示( 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)))  

第二个情节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM