Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot line behind barplot

Tags:

plot

r

I would like to create a barplot where the bars are plotted on top of the horizontal line.

The following code accomplishes this:

y <- c(1,2,3,5)
barplot(y)
abline(h=mean(y))
barplot(y, add=T)

However, I'm concerned that the add=T parameter in barplot(), if used repeatedly, can introduce printing artifacts. I'm curious if there's an alternative to the above code (though the above code may be the fastest method).

like image 642
andrewj Avatar asked Nov 28 '12 03:11

andrewj


People also ask

How do I add a horizontal line to a bar graph in R?

To create horizontal lines for each bar in a bar plot of base R, we can use abline function and pass the same values as in the original barplot with h argument that represents horizontal with different color to make the plot a little better in terms of visualization.

What is the black line in SNS barplot?

In your data, it is likely the Standard Deviation or STD line.


1 Answers

You could just plot nothing in your first call:

y <- c(1,2,3,5)
barplot(
  rep(NA, length(y)),
  ylim = pmax(range(y), 0),
  axes = FALSE
)
abline(h = mean(y))
barplot(y, add = TRUE)

Barplot with horizontal line behind the bars, as desired

like image 51
thelatemail Avatar answered Oct 30 '22 22:10

thelatemail