如何使用ggplot2在水平线下方透明地阴影一个区域?

4

这是原始图像。

Original Image

这是我用来生成上面图片的代码。
## Employees Wise Sales Report: MAY 2014-LYNDA Best Visualization Assignment
setwd('d:/dataset/lynda')
empwisedata=read.csv('income.csv',header=T,sep=",")
names(empwisedata)
attach(empwisedata)

Minimum=c(min(Person1),min(Person2),min(Person3),min(Person4),min(Person5))
Average=c(mean(Person1),mean(Person2),mean(Person3),mean(Person4),mean(Person5))
Maximum=c(max(Person1),max(Person2),max(Person3),max(Person4),max(Person5))
attach(Average)

library(ggplot2)
library(reshape2)
df = melt(data.frame(Minimum,Average,Maximum,Employees=c("Person1", "Person2","Person3","Person4","Person5")),variable.name="IncomeLevels")
df$Employees<-factor(df$Employees,levels = df$Employees[order(Average)])

p=ggplot(df, aes(Employees, value, fill=IncomeLevels)) +   geom_bar(position="dodge",stat="identity")

p + geom_hline(yintercept=mean(Average))+scale_fill_manual(values=c("red","orange","dark green"))+labs(size= "Nitrogen", x = "Employees of ACME Widgets",y = "Income in USD", title = "ACME WIDGETS :: Employees Wise Sales Report-MAY 2014 ")

我想在图表中的水平线下填充颜色。我尝试使用geom_rect通过修改上述代码的最后一行来实现。
p+geom_hline(yintercept=mean(Average)) + scale_fill_manual(values=c("red","orange","dark green"))+labs(size= "Nitrogen", x = "Employees of ACME Widgets",y = "Income in USD", title = "ACME WIDGETS :: Employees Wise Sales Report-MAY 2014 ") + geom_rect(xmin=0,xmax=200,ymin=0,ymax=mean(Average),fill="blue")

并获得了以下图像。

enter image description here

我不需要深蓝色。我需要透明度,以便查看普通条形图(黄色)。我尝试了不同的 alpha 水平,但没有效果。感谢您的帮助。
解决方案: 根据 LukeA 的建议,我修改了代码的最后一行。代码如下:

p+geom_hline(yintercept=mean(Average))+scale_fill_manual(values=c("red","orange","dark green"))+labs(size= "Nitrogen", x = "Employees of ACME Widgets",y = "Income in USD", title = "ACME WIDGETS :: Employees Wise Sales Report-MAY 2014 ")+  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = mean(Average), fill = "blue", alpha = .1, color = NA)

我已经按照要求进行了翻译:

如下所示,已获得所需的输出图。

enter image description here

谢谢大家。

1个回答

11
试试这样做。
library(ggplot2)
ggplot(mtcars, aes(mpg)) + 
  geom_histogram() + 
  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 1, fill = "blue", alpha = .5, color = NA)

enter image description here


@VeeramaniNatarajan 如果您发现lukeA的答案最有帮助,您可能会想要勾选绿色的勾号(位于他们回复的左上角)。 - Tyler Rinker
有人知道如何在x轴和深色水平线之间添加图例吗? - Veeramani Natarajan
在这种情况下,您需要使用美学映射。例如 ggplot() + geom_histogram(data = mtcars, aes(mpg)) + geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 1, fill = "fill"), alpha = .5, color = NA) + scale_fill_manual(values = "blue") + labs(fill = "Legend") - lukeA
如果x轴是日期时间,并且我想要一个类似的半透明框来表示任何时间范围,是否有Inf的替代方法或其他简单的方法?我得到了错误:无效输入:time_trans仅适用于POSIXct类的对象 - dbo
@doconnor library(ggplot2) o <- "1970-01-01"; ggplot(mtcars, aes(as.POSIXct(mpg, origin = o))) + geom_histogram() + annotate("rect", xmin = as.POSIXct(-Inf, origin = o), xmax = as.POSIXct(Inf, origin = o), ymin = -Inf, ymax = 1, fill = "blue", alpha = .5, color = NA)? - lukeA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接