Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add text to horizontal barplot in R, y-axis at different scale?

Tags:

plot

r

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))
like image 846
CCID Avatar asked Nov 18 '10 16:11

CCID


People also ask

How do I add a horizontal line to a Barplot in R?

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.

How do I rotate a Barplot label in R?

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.

How do I change the width of a column in a Barplot in R?

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.

How do I adjust a Barplot in R?

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.


1 Answers

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
like image 197
Aniko Avatar answered Sep 22 '22 20:09

Aniko