I'm trying to add some text to the right hand side of a horizontal barplot at the same heights as each bar, however, both text() and axis() don't seem to plot this at the heights corresponding to each bar.
Here's a similar barplot
x <- runif(10, 0,1)
y <- matrix(c(x, 1-x), nrow=2, ncol=10, byrow=TRUE)
barplot(y, horiz=TRUE, beside=FALSE, names.arg=seq(1,10,1), las=1, xlim=c(0, 1.2))
Neither of these two options align properly, how does the scaling work here?
axis(4, at=seq(1,10,1), labels=seq(1,10,1))
text(1.1, seq(1,10,1), labels=seq(1, 10, 1))
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.
Method 1: Using barplot() To display all the labels, we need to rotate the axis, and we do it using the las parameter. To rotate the label perpendicular to the axis we set the value of las as 2, and for horizontal rotation, we set the value as 1. Secondly, to increase the font size of the labels we use cex.
To Increase or Decrease width of Bars of BarPlot, we simply assign one more width parameter to geom_bar() function. We can give values from 0.00 to 1.00 as per our requirements.
R barplot() – Set Width for Bars in Bar Plot To set width for bars in Bar Plot drawn using barplot() function, pass the required width value for width parameter in the function call. width parameter is optional and can accept a single value or a vector to set different width for different bars in the bar plot.
By chacking the documentation of barplot
, you can see that it has an invisible return value: the midpoints of the bars. You can use those to add additional information to the plot.
x <- runif(10, 0,1)
y <- matrix(c(x, 1-x), nrow=2, ncol=10, byrow=TRUE)
bp <- barplot(y, horiz=TRUE, beside=FALSE, names.arg=seq(1,10,1), las=1,
xlim=c(0, 1.2))
text(x, bp, signif(x,2), pos=4)
bp
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