R 合并两个情节,使系列的颜色在图例中一致
我想把两块地放在另一块上。这两个图共享相同的X轴和Y轴。唯一的区别是第一个图是条形图,第二个图是折线图R 合并两个情节,使系列的颜色在图例中一致,r,ggplot2,R,Ggplot2,我想把两块地放在另一块上。这两个图共享相同的X轴和Y轴。唯一的区别是第一个图是条形图,第二个图是折线图 library("ggplot2") p1 <- ggplot(result_a, aes(x=type,y=as.numeric(num_excluded),fill=as.factor(year),width=.5)) + geom_bar(position = "stack", stat="identity")+ coord_flip() p1 <- p1
library("ggplot2")
p1 <- ggplot(result_a, aes(x=type,y=as.numeric(num_excluded),fill=as.factor(year),width=.5)) +
geom_bar(position = "stack", stat="identity")+
coord_flip()
p1 <- p1 + guides(fill=guide_legend(title="Legend:")) +
scale_fill_brewer(palette="Set1")
p2 <- ggplot(result_a, aes(x=type,y=as.numeric(total),
group=as.factor(year),color=as.factor(year),
width=.5)) +
geom_line()+geom_point()+
coord_flip()
你是说这样的事吗
ggplot(result_a) +
geom_bar(aes(x = type, y = as.numeric(num_excluded),
fill = as.factor(year), width=.5), position = "stack", stat = "identity") +
geom_line(aes(x = type, y = as.numeric(total),
group = as.factor(year), color = as.factor(year),
width = .5), size = 2) +
coord_flip()+
guides(fill = guide_legend(title = "Legend:")) +
scale_fill_brewer(palette = "Set1") +
scale_color_brewer(palette = "Set1", guide = FALSE)
你是说这样的事吗
ggplot(result_a) +
geom_bar(aes(x = type, y = as.numeric(num_excluded),
fill = as.factor(year), width=.5), position = "stack", stat = "identity") +
geom_line(aes(x = type, y = as.numeric(total),
group = as.factor(year), color = as.factor(year),
width = .5), size = 2) +
coord_flip()+
guides(fill = guide_legend(title = "Legend:")) +
scale_fill_brewer(palette = "Set1") +
scale_color_brewer(palette = "Set1", guide = FALSE)
你能发布一个
dput(结果)
?也许会有帮助。对于颜色,您应该能够手动将它们设置为相同的调色板。@MikeyMike:完成。@Haboryme:谢谢,但我还没有找到这个线程中颜色问题的解决方案。我弄错了什么吗?@cartlefish44:它们有相同的度量单位,但p2的y值比p1的大。因此,p2的行最终将出现在p1的顶部(如果使用coord\u flip()
),则会出现在右边更多的行。您可以发布dput(result\u a)
?也许会有帮助。对于颜色,您应该能够手动将它们设置为相同的调色板。@MikeyMike:完成。@Haboryme:谢谢,但我还没有找到这个线程中颜色问题的解决方案。我弄错了什么吗?@cartlefish44:它们有相同的度量单位,但p2的y值比p1的大。因此,p2的行最终将显示在p1的顶部(如果使用coord_flip()
),则显示在右侧更多。谢谢你,Marta。但是,我想将这些图表合并到一个绘图上。因此,p2的行将位于p1的顶部(如果使用coord_flip(),则会向右移动更多)
。你知道怎么做吗?@FiofanS,我已经编辑了我的答案。你在找这样的东西吗?是的,当然!只是一个小细节:我还想分享图例及其颜色。是否可以使用一个对应于两个图表的图例?@FiofanS,我再次编辑。我刚刚删除了一个带有guide=FALSE 。谢谢你,玛尔塔。不过,我想将这些图表合并到一个绘图上。因此,p2的线条将显示在p1的顶部(如果使用coord_flip(),则显示在右侧更多)
。你知道怎么做吗?@FiofanS,我已经编辑了我的答案。你在找这样的东西吗?是的,当然!只是一个小细节:我还想分享图例及其颜色。是否可以使用一个对应于两个图表的图例?@FiofanS,我再次编辑。我刚刚删除了一个带有guide=FALSE 。