Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a horizontal line above a bar chart using ggplot?

Tags:

r

ggplot2

I would like to add a horizontal line above a bar plot. Currently I am able to add dots using geom_points:

data <- ddply(diamonds, .(cut, color), summarise, mean_carat = mean(carat))
ggplot(data, aes(color, mean_carat,fill=cut)) +
geom_bar(stat="identity", position="dodge") +
geom_point(data=data, aes(color, mean_carat, fill=cut))

The red arrow in the image below shows what I would like to add:

Thanks!

like image 759
alfakini Avatar asked May 11 '15 00:05

alfakini


1 Answers

Try to add something like

data <- ddply(diamonds, .(cut, color), summarise, mean_carat = mean(carat))
data2 <- ddply(data, ~color, summarise, max_carat = max(mean_carat))
C <- merge(data,data2, by = c('color'))

    ggplot(data, aes(color, mean_carat,fill=cut)) +
      geom_bar(stat="identity", position="dodge") +
      geom_point(data=data, aes(color, mean_carat)) +
      geom_errorbar(data  = C, aes(y=max_carat, ymax=max_carat, ymin=max_carat))

RPLOT

like image 57
MichaelVE Avatar answered Oct 30 '22 02:10

MichaelVE