【发布时间】:2019-06-13 09:35:58
【问题描述】:
我也在尝试制作带有折线图的条形图。该图创建得很好,但图例不想将线图添加到图例中。
我尝试了很多不同的方法将这些添加到图例中,包括:
ggplot Legend Bar and Line in Same Graph
这些都没有奏效。 show.legend 在geom_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 相当陌生,所以如果需要任何进一步的信息,或者如果这个问题可以更好地表达,请告诉我。
非常感谢任何帮助。
【问题讨论】: