R 中聚类堆积条形图中每个组的单独标记

Individual labelling for each group in a clustered stacked bar graph in R

使用此答案的帮助:

test  <- data.frame(person=c("Q8.3. Patients presenting to me, who are suitable \n for referral to social prescribing services, \n have decreased.\n GP (n = 72) \n LW (n = 101)",
                             "Q8.5. Patients feel well-supported during COVID-19 \n for their emotional and social needs.\n GP (n = 70) \n LW (n = 125)",
                             "Q9.1. Less patients are interested in being \n referred to social prescribing services.\n GP (n = 55) \n LW (n = 100)",
                             "Q9.4. Online services are available to replace community \n groups and statutory services that are \n not available. \n GP (n = 60) \n LW (n = 133)",
                             "Q9.6. Link workers have great influence on the services \n people are referred to.\n GP (n = 58) \n LW (n = 131)"), 
                    value1=c(22.22, 8.57, 23.63, 31.66, 79.31),  # GP Agree   
                    value2=c(54.16, 77.14, 58.18, 40, 13.79) ,    # GP Disagree
                    value3=c(23.61, 14.28, 18.18, 28.33, 6.89),     # GP Neutral
                    value4=c(19.8, 48, 9, 51.87, 69.46),  # LW Agree
                    value5=c(64.35,34.4,70,28.57,14.5),     # LW Disagree
                    value6=c(15.84,17.6,21,19.54,16.03))     # LW Neutral

library(reshape2) # for melt

melted <- melt(test, "person")

melted$cat <- ''
melted[melted$variable == 'value1' | melted$variable == 'value2' | melted$variable == 'value3',]$cat <- "GP"
melted[melted$variable == 'value4' | melted$variable == 'value5'| melted$variable == 'value6',]$cat <- "LW"
melted$Response <- ''
melted[melted$variable %in% sprintf("value%i",c(1,4)),]$Response <- "Agree"
melted[melted$variable %in% sprintf("value%i",c(2,5)),]$Response <- "Disagree"
melted[melted$variable %in% sprintf("value%i",c(3,6)),]$Response <- "Neutral"


melted$Response <- factor(melted$Response, c("Agree", "Neutral","Disagree"))

p = ggplot(melted, aes(x = cat, y = value, fill = Response))
p + geom_bar(stat = 'identity', position = 'stack') +
  geom_text(aes(label = paste0(value, "%")), position = position_stack(vjust = .5)) +
  ylab("Percentage of responses")+
  xlab("Population")+
  facet_grid(person~.) + 
  scale_fill_manual(values = c("#488f31","#ffeb8a","#de425b")) + 
  theme(panel.background = element_rect(fill = 'white'),
        strip.text.y = element_text(angle = 0)) +
  coord_flip()

我能够生成以下图表:

我的问题是:如何获取组(GP、LW)的标签以在其旁边显示样本量?我想知道我可以在代码的哪个位置添加这样一个数组来标记图表。

例如,第二个柱将有 GP (n = 72),第一个柱将在右侧写上 LW (n = 101),而不仅仅是 GP 和 LW。

谢谢。

这可以这样实现:

  1. 从您 person 变量中提取包括样本大小的类别标签,例如使用string::str_extract 并将其分配给 cat.
  2. scales = "free_y" 添加到 facet_grid

EDIT 可以使用例如从问题中删除样本大小gsub:


library(reshape2) # for melt
library(ggplot2)
library(stringr)

melted$cat <- ifelse(melted$cat == "GP", 
                     stringr::str_extract(melted$person, "GP \(n = \d+\)"), 
                     stringr::str_extract(melted$person, "LW \(n = \d+\)"))

melted$person <- gsub("(GP|LW) \(n = \d+\)", "", melted$person)

p = ggplot(melted, aes(x = cat, y = value, fill = Response))
p + geom_bar(stat = 'identity', position = 'stack') +
  geom_text(aes(label = paste0(value, "%")), position = position_stack(vjust = .5)) +
  ylab("Percentage of responses")+
  xlab("Population")+
  facet_grid(person~., scales = "free_y") + 
  scale_fill_manual(values = c("#488f31","#ffeb8a","#de425b")) + 
  theme(panel.background = element_rect(fill = 'white'),
        strip.text.y = element_text(angle = 0)) +
  coord_flip()