Adding a line to a barplot with two different x coordinates in R -
i have barplot , add plot in barplot, according different scale values of x-axis can not properly. first line cross first bar , second line should cross second , third bars, , last line cross fourth , fifth bars. how can in r? wrote following code:
barplot(c(2.5, .5, .5, 3.5, 6), names.arg=c("0-1","1-2", "2-3", "3-4", "4-5")) lines(c(1, 2, 3,4 ,5), c(3, .1, .1, 4, 6.5), lty=2, lwd=2)
it bit complicated understand want... try these 2 hints:
the x-values @ bars plotted returned function barplot. so, use them in latter plot, can store them with
bp = barplot(...)and use them latter in call lines:
lines(bp, y.data, ...)you find parts of lines going above bars not plotted, because y values cropped default when view initialize barplot. there several possible workarounds:
bp = barplot(..., ylim=range(data)+c(-1,1) ) # set y-limits during call barplotor:
lines(..., xpd=t) # allow drawing in plot margin
finally, minimal working example displayed below:
data = c(2.5, .5, .5, 3.5, 6) bp = barplot(data, names.arg=c("0-1","1-2", "2-3", "3-4", "4-5"), ylim = range(data)+c(-1,1) ) lines(bp, c(3, .1, .1, 4, 6.5), lty=2, lwd=2) 
Comments
Post a Comment