在ggplot中的图表旁边显示总计和均值

问题描述

enter image description here

我正在尝试将我的均值和总计放在条形图的左侧,如上例所示。 我在ggplot中创建了条形图。下面的图片代码。关于如何获取均值和总计以正确显示位置的任何建议?可能是custom_annotations?

谢谢

enter image description here

  percentData = stotal %>% #stotal = survey data frame
    group_by(qtext,div,response) %>%  #qtext is my question text
    summarise(N = n()) %>% 
    mutate(prop = N/sum(N))
  percentData$prop = label_percent(accuracy = 1)(percentData$prop) #make percent from decimal
  percentData
  
  
  #colors
  myColors <- c("green4","springgreen2","yellow1","orange1","red1","black","black")
  
  ggplot(stotal)+
    geom_bar(aes(x = div,fill = response),position = 'fill',width = 0.5)+
    facet_grid(rows = vars(qtext))+
    scale_fill_manual (values = myColors)+
    coord_flip()+
    ylab('')+
    xlab('')+
    scale_y_continuous(labels = percent)+
    ggtitle(i)+
    geom_text(data = percentData,aes(fill = response,y = N,label = prop,x = div),position=position_fill(vjust=0.5))+
    theme(strip.text.y = element_text(size = 12,angle = 0,family = "serif"))

  

解决方法

一种实现此目的的方法是通过第二个ggplot制作表格,该表格可以通过例如patchwork。基本上,表格图只复制了一个类别的主图,使用分面来获得带有均值和总数的列布局,并摆脱了轴,网格,背景色,...

使用一些随机示例数据尝试以下操作:

library(ggplot2)
library(dplyr)
library(scales)
library(patchwork)

# Random example data
set.seed(42)

stotal <- data.frame(
  qtext = rep(c("A","B"),50),div = sample(c("University","KSAS-HUM"),100,replace = TRUE),response = sample(c("Poor","Fair","Good","Very good","Excellent"),replace = TRUE)
)
stotal$response <- factor(stotal$response,levels = c("Poor","Excellent"))

percentData = stotal %>% #stotal = survey data frame
  group_by(qtext,div,response) %>%  #qtext is my question text
  summarise(N = n()) %>% 
  mutate(prop = N/sum(N))
#> `summarise()` regrouping output by 'qtext','div' (override with `.groups` argument)
percentData$prop = label_percent(accuracy = 1)(percentData$prop) #make percent from decimal

#colors
myColors <- c("green4","springgreen2","yellow1","orange1","red1","black","black")

p1 <- ggplot(stotal)+
  geom_bar(aes(x = div,fill = response),position = 'fill',width = 0.5)+
  facet_grid(rows = vars(qtext))+
  scale_fill_manual (values = myColors)+
  coord_flip()+
  ylab('')+
  xlab('')+
  scale_y_continuous(labels = percent)+
  #ggtitle(i)+
  geom_text(data = percentData,aes(fill = response,y = N,label = prop,x = div),position=position_fill(vjust=0.5))+
  theme(strip.text.y = element_text(size = 12,angle = 0,family = "serif"))
#> Warning: Ignoring unknown aesthetics: fill


# Table plot

table_data <- stotal %>% 
  mutate(response = as.numeric(response)) %>% 
  group_by(qtext,div) %>%
  summarise(Mean = mean(response),"Total N" = n()) %>% 
  mutate(Mean = round(Mean,1)) %>% 
  tidyr::pivot_longer(-c(qtext,div),names_to = "var")
#> `summarise()` regrouping output by 'qtext' (override with `.groups` argument)

p2 <- ggplot(table_data,aes(x = div)) +
  geom_bar(color = "white",fill = "white",width = .5)+
  #geom_vline(color = "grey",xintercept = c(.5,1.5,2.5)) +
  geom_text(aes(y = 1,label = value),position=position_fill(vjust=0.5),size = 0.8 * 11 /.pt) + 
  facet_grid(qtext ~ var,switch = "y") +
  coord_flip() +
  labs(x = NULL,y = NULL) +
  theme_minimal() +
  theme(strip.text.y = element_blank()) +
  theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(),axis.text.x = element_text(color = "transparent"),axis.ticks.x = element_line(color = "transparent"),axis.title = element_blank(),panel.grid = element_blank(),panel.spacing.x = unit(0,"pt"))
            

# Glue together

p2 + p1 + plot_layout(widths = c(1,3))