r - How to shade a region under a horizontal line transparently using ggplot2? -


this original image.

original image

this code used produce above 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 ") 

i fill color under horizontal line in graph. had tried geom_rect modifying last line of above code follows.

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") 

and got following image.

enter image description here

i dont need dark blue. need transparency average bars (yellow coloured) viewed. had tried different alpha levels too. nothing works. appreciated.

solution: had modified last line of code per advice of lukea. code

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)

and got desired output plot mentioned below. enter image description here

thank all.

try this

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


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -