Vee*_*jan 1 r horizontal-line ggplot2
这是原始图像.

这是我用来生成上面图像的代码.
## 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 ")
Run Code Online (Sandbox Code Playgroud)
我想填充图表中水平线下的颜色.我通过修改上面代码的最后一行尝试了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")
Run Code Online (Sandbox Code Playgroud)
并得到以下图像.

我不需要深蓝色.我需要透明度,以便查看平均条纹(黄色).我曾尝试过不同的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)Run Code Online (Sandbox Code Playgroud)
并获得如下所述的所需输出图.

谢谢你们.
尝试这样的事情
library(ggplot2)
ggplot(mtcars, aes(mpg)) +
geom_histogram() +
annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 1, fill = "blue", alpha = .5, color = NA)
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
2026 次 |
| 最近记录: |