我如何在ggplot命令中激活两个不同的scale_fill_manual [英] How can I have two different scale_fill_manual active in a ggplot command

查看:392
本文介绍了我如何在ggplot命令中激活两个不同的scale_fill_manual的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题来自我的从那里的答案中,我现在可以使用geom_rect为绘图提供五种不同颜色的背景.最重要的是,我想绘制一个使用两种不同颜色的条形图.我可以分别完成每个任务,但是当我尝试将它们组合在一起时,scale_fill_manual命令会发生冲突.

From the answers there, I am now able to use geom_rect to give a background to my plot that has five different colours. On top of that I'd like to plot a barchart that uses two different colours. I can do each of these tasks separately, but when I try to combine them the scale_fill_manual commands clash.

这就是我要尝试的:

scores = data.frame(category = 1:4, percentage = c(34,62,41,44), type = c("a","a","a","b"))
rects <- data.frame(ystart = c(0,25,45,65,85), yend = c(25,45,65,85,100), col = letters[1:5])
labels = c("ER", "OP", "PAE", "Overall")
medals = c("navy","goldenrod4","darkgrey","gold","cadetblue1")

ggplot() + 
geom_rect(data = rects, aes(xmin = -Inf, xmax = Inf, ymin = ystart, ymax = yend, fill=col), alpha = 0.3) + 
scale_fill_manual(values=medals) +
opts(legend.position="none") + 
geom_bar(data=scores, aes(x=category, y=percentage, fill=type), stat="identity") +
#scale_fill_manual(values = c("indianred1", "indianred4")) +
scale_x_continuous(breaks = 1:4, labels = labels) 

如所写,这使两种条形图颜色与前两种背景色相同.删除第二个scale_fill_manual命令(倒数第二行)上的"#"会覆盖背景颜色命令,以使条形成为我想要的颜色,但使背景只有我想要的两个颜色在barchart中.

As written, this makes the two barchart colours the same as the first two background colours. Removing the "#" on the second scale_fill_manual command (penultimate line) overrides the background colour commands to make the bars the colours I want but makes the background have just the two colours I want in the barchart.

如何将一个scale_fill_manual命令应用于geom_rect背景,将另一个scale_fill_manual命令应用于geom_bar条形图(或者如何通过其他方式实现相同的效果)?

How can I have one scale_fill_manual command applying to the geom_rect background and the other to the geom_bar barchart (or how can I achieve the same effect by other means)?

推荐答案

问题是您在rectsscores中都使用了"a""b",因此它们被映射为相同的颜色.由于矩形似乎是占位符值,因此请将它们更改为比scores中的任何内容晚排序的独特内容.

The problem is that you are using "a" and "b" in both rects and scores, so they get mapped to the same color. Since the rectangles seem to be placeholder values, change them to something distinct that sorts later than anything in scores.

rects$col <- c("Z1","Z2","Z3","Z4","Z5")

现在您可以用全部(7)种颜色制作一个scale_fill_manual.

Now you can make one scale_fill_manual with all (7) colors.

ggplot() + 
geom_rect(data = rects, aes(xmin = -Inf, xmax = Inf, ymin = ystart, 
                            ymax = yend, fill=col), alpha = 0.3) + 
opts(legend.position="none") + 
geom_bar(data=scores, aes(x=category, y=percentage, fill=type), stat="identity") +
scale_fill_manual(values=c("indianred1", "indianred4", medals)) +
scale_x_continuous(breaks = 1:4, labels = labels) 

这篇关于我如何在ggplot命令中激活两个不同的scale_fill_manual的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆