Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
870 views
in Technique[技术] by (71.8m points)

r - How to automate positioning of inner labels within a stacked barplot?

I frequently have to produce stacked bar plots with labels. The way I've been coding the labels is very time intensive and I wondered if there was a way to code things more efficiently. I would like the labels to be centered on each section of the bars. I'd prefer base R solutions.

stemdata <- structure(list( #had to round some nums below for 100% bar

A = c(7, 17, 76), 
B = c(14, 10, 76),
C = c( 14, 17, 69),
D = c( 4, 10, 86), 
E = c( 7, 17, 76),
F = c(4, 10, 86)), 

.Names = c("Food, travel, accommodations, and procedures",
         "Travel itinerary and dates",
        "Location of the STEM Tour stops",
         "Interactions with presenters/guides",
         "Duration of each STEM Tour stop",
        "Overall quality of the STEM Tour"
         ), 
class = "data.frame", 
row.names = c(NA, -3L)) #4L=number of numbers in each letter vector#

# attach(stemdata)
print(stemdata)
par(mar=c(0, 19, 1, 2.1)) # this sets margins to allow long labels
barplot(as.matrix(stemdata), 

    beside = F, ylim = range(0, 10), xlim = range(0, 100),
    horiz = T, col=colors,  main="N=29", 
    border=F, las=1, xaxt='n', width = 1.03)

text(7, 2, "14%")
text(19, 2, "10%")
text(62, 2, "76%")

text(7, 3.2, "14%")
text(22.5, 3.2, "17%")
text(65.5, 3.2, "69%")

text(8, 4.4, "10%")
text(55, 4.4, "86%")

text(3.5, 5.6, "7%")
text(15, 5.6, "17%")
text(62, 5.6, "76%")

text(9, 6.9, "10%")
text(55, 6.9, "86%")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Staying base R as OP requested, we can easily automate the inner label positioning (i.e. x coordinates) within a small function.

xFun <- function(x) x/2 + c(0, cumsum(x)[-length(x)])

Now, it's good to know that barplot invisibly trows the y coordinates, we can catch them by assignment (here byc <- barplot(.)).

Eventually, just assemble coordinates and labels in data frame labs and "loop" through the text calls in a sapply. (Use col="white" or col=0 for white labels as wished in the other question.)

# barplot
colors <- c("gold", "orange", "red")
par(mar=c(2, 19, 4, 2) + 0.1)  # expand margins

byc <- barplot(as.matrix(stemdata), horiz=TRUE, col=colors, main="N=29",  # assign `byc`
               border=FALSE, las=1, xaxt='n')


# labels
labs <- data.frame(x=as.vector(sapply(stemdata, xFun)),  # apply `xFun` here 
                   y=rep(byc, each=nrow(stemdata)),  # use `byc` here
                   labels=as.vector(apply(stemdata, 1:2, paste0, "%")), 
                   stringsAsFactors=FALSE)

invisible(sapply(seq(nrow(labs)), function(x)  # `invisible` prevents unneeded console output
  text(x=labs[x, 1:2], labels=labs[x, 3], cex=.9, font=2, col=0)))

# legend  (set `xpd=TRUE` to plot beyond margins!)
legend(-55, 8.5, legend=c("Medium","High", "Very High"), col=colors, pch=15, xpd=TRUE)

par(mar=c(5, 4, 4, 2) + 0.1)  # finally better reset par to default

Result

enter image description here

Data

stemdata <- structure(list(`Food, travel, accommodations, and procedures` = c(7, 
17, 76), `Travel itinerary and dates` = c(14, 10, 76), `Location of the STEM Tour stops` = c(14, 
17, 69), `Interactions with presenters/guides` = c(4, 10, 86), 
    `Duration of each STEM Tour stop` = c(7, 17, 76), `Overall quality of the STEM Tour` = c(4, 
    10, 86)), class = "data.frame", row.names = c(NA, -3L))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...