Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw vertical lines in barplot?

Tags:

r

I want to draw a vertical line at specific positions of the x-axis, but the x-positions of the lines aren't correct. How can I solve this ?

x <- c(0,0,0,4,5,6)
barplot(x, names.arg=1:length(x))
abline(v=1:length(x), col="red")
abline(v=c(5.5), col="blue")
like image 383
user Avatar asked Mar 03 '23 23:03

user


1 Answers

You have to save the result of barplot. Then use those values to plot the vertical lines.

x <- c(0,0,0,4,5,6)
bp <- barplot(x, names.arg = seq_along(x))
abline(v = bp, col = "red")
abline(v = 5.5, col = "blue")

enter image description here

Note that the blue line was plotted twice and therefore became violet. So remove the value 5.5 from the first call to abline.

bp <- barplot(x, names.arg = seq_along(x))
abline(v = bp[bp != 5.5], col = "red")
abline(v = 5.5, col = "blue")

enter image description here

like image 133
Rui Barradas Avatar answered Mar 05 '23 15:03

Rui Barradas