Adrian
Adrian

Reputation: 9793

R: adding vertical line to barplot

mydat <- c(rep(4:10, each = 3), rep(1:2, each = 2))
barplot(table(mydat))
abline(v = 3, col = "blue")

This gives me the following plot: enter image description here I want a vertical line at x=3. However, because of the way the x-axis is spaced out, the vertical line doesn't appear to be in the right place. How can I fix this? i.e. I want my plot to look something like this:

enter image description here

Upvotes: 2

Views: 834

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76402

To plot a line between the 2nd and the 3rd bars, assign the return value of barplot and then plot in the mean value of the 2nd and 3rd of those values.

bp <- as.vector(barplot(table(mydat)))
abline(v = mean(bp[2:3]), col = "blue")

enter image description here

Upvotes: 5

Related Questions