[英]R: Using Alpha to Control Fill
我正在尝试使用alpha来控制图形填充的透明度。 我知道alpha函数已被移动到scale包,但我仍然无法使它工作。
数据集:
library(scales)
library(ggplot2)
Groups <- data.frame(Level=c("High", "Above Average", "Average", "Below Average", "Low"),
Start=c(7.5, 5.5, 4.5, 2.5, .5), End=c(11.5, 7.5, 5.5, 4.5, 2.5))
Groups$Start<-as.numeric(as.character(Groups$Start))
d <- data.frame(x=1:10, y=1:10)
basicBox <- ggplot(d, aes(factor(x), y)) + geom_boxplot()
basicBox +
geom_rect(aes(NULL, NULL, xmin=Start, xmax=End, fill=Level), ymin=0, ymax=20, data=Groups) +
scale_fill_manual(values=alpha(c("red", "blue", "green", "grey", "purple"), 0.2))
类似的代码可以在Hadley Wickham的ggplot2的第86页找到。 当我没有加载scale库时,我得到并且错误地说R不能识别alpha函数,但即使在我加载了scale之后我也无法使alpha工作正常。
如果我得到你所要求的,它应该是这样的:
basicBox +
geom_rect(aes(NULL, NULL, xmin=Start, xmax=End, fill=Level),
ymin=0, ymax=20, data=Groups, alpha=0.2) +
scale_fill_manual(values=c("red", "blue", "green", "grey", "purple"))
我将alpha参数放在geom_rect
,并没有将它用作函数。
不确定你的意思是“不能让alpha工作正常”; 你的代码运行正常。
小建议:
ggplot(d, aes(factor(x), y)) +
geom_rect(aes(NULL, NULL, xmin=Start, xmax=End, fill=Level),
alpha = 0.2, ymin=-Inf, ymax=Inf,data=Groups) +
geom_boxplot() +
scale_fill_brewer(palette="Pastel1")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.