edstatsuser
edstatsuser

Reputation: 220

How do I find the correct coordinates to align labels with barplot bars?

I'm creating a graphic that has a few different graph elements, using layout() to define plotting regions. I have a separate region for labels that need to align to bars on a barplot in an adjacent plotting region.

I can take a guess at where to plot the labels so that they line up - but the number of these locations will vary so this is not an ideal solution.

Here's an example of what I'm trying to do:

labs <- paste("Some text", letters[1:9])
datA <- table(sample(letters[1:9], size=200, replace=TRUE, prob=rep(c(0.1,0.2,0.3),3)))
layout(matrix(c(1,2,3,3), 2, 2, byrow=TRUE), widths=c(1,2), heights=c(6,1))
plot.new()
text(x=1, y=seq(0.05,1.0,0.111), labels=labs, adj=1, cex=1.4)
barplot(datA, horiz=TRUE, las=1, axes=F, yaxt="n")

How can I find the correct values to plot the labels?

(I'm aware that it looks like this can be solved by just plotting the labels with the barplot - this is not a viable solution for what I'm doing).

Upvotes: 1

Views: 173

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 270428

The output of barplot gives the heights so:

bp <- barplot(datA, horiz=TRUE, las=1, axes=F, yaxt="n")
text(0*bp, bp, labs, col = "blue", pos = 4)

screenshot

Upvotes: 1

Related Questions