3

This is the original image.

Original Image

This is the code i used to produce the 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 would like to fill the color under the horizontal line in the graph. i had tried geom_rect by modifying the last line of above code as 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 the following image.

enter image description here

I dont need dark blue. I need transparency so that average bars (Yellow coloured) also viewed. I had tried with different alpha levels too. But nothing works. Your help is appreciated.

Solution: i had modified the last line of the code as per the advice of LukeA. The code is

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 the desired output plot as mentioned below. enter image description here

Thank you all.

Veeramani Natarajan
  • 192
  • 2
  • 4
  • 11

1 Answers1

10

Try something like 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

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • @VeeramaniNatarajan you may want to check the green checkmark for lukeA's answer if you found it most helpful (it's in the upper left of their response). – Tyler Rinker Jul 14 '15 at 11:53
  • Does anyone know how to add legend to this rectangle bar lying between x axis and dark horizontal line – Veeramani Natarajan Jul 14 '15 at 13:05
  • In that case, you'd have to use aesthetic mappings. E.g. `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 Jul 14 '15 at 13:18
  • If the x-axis were datetime, and I wanted a similar translucent box for any time range, is there a substitute for `Inf`, or another easy method? I get `Error: Invalid input: time_trans works with objects of class POSIXct only` – dbo Nov 16 '18 at 21:58
  • @doconnor `library(ggplot2) o <- "1970-01-01"; ggplot(mtcars, aes(as.POSIXct(mpg, orig = o))) + geom_histogram() + annotate("rect", xmin = as.POSIXct(-Inf, orig = o), xmax = as.POSIXct(Inf, orig = o), ymin = -Inf, ymax = 1, fill = "blue", alpha = .5, color = NA)`? – lukeA Nov 16 '18 at 22:54