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

泪湿孤枕 提交于 2019-12-29 08:54:06

问题


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%")

回答1:


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

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))



回答2:


Would you consider a tidyverse solution?

library(tidyverse) # for dplyr, tidyr, tibble & ggplot2

stemdata %>% 
  rownames_to_column(var = "id") %>% 
  gather(Var, Val, -id) %>% 
  group_by(Var) %>% 
  mutate(id = factor(id, levels = 3:1)) %>% 
  ggplot(aes(Var, Val)) + 
  geom_col(aes(fill = id)) + 
  coord_flip() + 
  geom_text(aes(label = paste0(Val, "%")), 
            position = position_stack(0.5))

Result:



来源:https://stackoverflow.com/questions/56659665/how-to-automate-positioning-of-inner-labels-within-a-stacked-barplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!