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).
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.
In your data, it is likely the Standard Deviation or STD line.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With