在ggplot2中使用facet_grid()函数时,如何使用labeller()函数获取列总计显示在构面的标签中 [英] how to use labeller() functions to get column totals to appear in the label of a facet when using the facet_grid() function in ggplot2

查看:258
本文介绍了在ggplot2中使用facet_grid()函数时,如何使用labeller()函数获取列总计显示在构面的标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个数据集,可为我的问题提供背景信息:

here's a data set to give context to my question:

library(tidyr); library(dplyr); library(ggplot2)
set.seed(1)
dfr2 <- tibble(x1 = factor(sample(letters[1:3], 50, replace = T), levels=letters[1:3]),
             x2 = factor(sample(letters[1:2], 50, replace = T), levels=letters[1:2]),
             x3 = factor(sample(letters[1:3], 50, replace = T), levels=letters[1:3]),
             grpA = factor(sample(c("grp1","grp2"),50, prob=c(0.3, 0.7) ,replace=T), levels = c("grp1", "grp2")),
             grpB = factor(sample(c("grp1","grp2"),50, prob=c(0.6, 0.4) ,replace=T), levels = c("grp1", "grp2"))
             )

head(dfr2)

这是一个准备绘图数据的函数:

here's a function that prepares the data for plotting:


plot_data_prepr <- function(dat, groupvar, mainvar){
  
  groupvar <- sym(groupvar)
  mainvar <- sym(mainvar)
  
  plot_data <- dat %>% 
    group_by(!!groupvar) %>% 
    count(!!mainvar, .drop = F) %>% drop_na() %>% 
    mutate(pct = n/sum(n),
         pct2 = ifelse(n == 0, 0.005, n/sum(n)),
         grp_tot = sum(n),
         pct_lab = paste0(format(pct*100, digits = 1),'%'),
         pct_pos = pct2 + .02)
  
  return(plot_data)
}

此函数的正常用法:


plot_data_prepr(dat = dfr2, groupvar = "grpA", mainvar = "x1")

我的目标是使用带有facet_grid()的labeller函数来获取在plot_data_prepr()函数内部计算的'grp_tot'变量,以将其粘贴到facet_grid()调用中的正确facet上,从而为facet提供两个标签最终将是'grp1(N = 20)','grp2(N = 30)'.

My goal is to use a labeller function with facet_grid() to get the 'grp_tot' variable calculated inside the plot_data_prepr() function to be pasted to the correct facet in the facet_grid() call such that the two labels for the facets would end up being 'grp1 (N = 20)' , 'grp2 (N = 30)'.

我可以成功地将一个字符串附加到因子级别:

I can successfully append a string to the factor level:


plusN <- function(string) {
  label <- paste0(string, ' (N = ',')')
  label
}

ggplot(plot_data_prepr(dfr2, "grpA", "x1"),
                 aes(x = x1, y = pct2, fill = x1)) +
      geom_bar(stat = 'identity') +
      ylim(0,1) +
      geom_text(aes(label=pct_lab, y = pct_pos + .02)) +
      facet_grid(. ~ grpA, labeller = labeller(grpA = plusN)) 

但是当我尝试将评估版的"grp_tot"变量粘贴到plusN函数时,它找不到该变量.我想我需要以某种方式延迟plusN函数中对'grp_tot'的求值,直到在facet_grid()内部调用它为止,但是 我不确定该怎么做:

but when I try to paste in the evaluated version of the 'grp_tot' variable to the plusN function, it can't find the variable. I think I need to somehow delay the evaluation of 'grp_tot' in the plusN function until it is called inside the facet_grid(), but I'm not sure how to do that:


plusN <- function(string) {
  label <- paste0(string, ' (N = ',eval.parent(grp_tot),')')
  label
}

ggplot(plot_data_prepr(dfr2, "grpA", "x1"),
                 aes(x = x1, y = pct2, fill = x1)) +
      geom_bar(stat = 'identity') +
      ylim(0,1) +
      geom_text(aes(label=pct_lab, y = pct_pos + .02)) +
      facet_grid(. ~ grpA, labeller = labeller(grpA = plusN)) 

我希望有人可以帮助我.

I hope someone might be able to help me.

谢谢.

推荐答案

只需最少的修改,以下代码(仅最后一个ggplot)

With minimal modification, the following code (only last ggplot)

dd <- plot_data_prepr(dat = dfr2, groupvar = "grpA", mainvar = "x1")

lookup <- unique(dd$grp_tot)

plusN <- function(string) {
  label <- paste0(string, ' (N = ',lookup,')')
  label
}

ggplot(plot_data_prepr(dfr2, "grpA", "x1"),
       aes(x = x1, y = pct2, fill = x1)) +
  geom_bar(stat = 'identity') +
  ylim(0,1) +
  geom_text(aes(label=pct_lab, y = pct_pos + .02)) +
  facet_grid(. ~ grpA, labeller = labeller(grpA = plusN)) 

提供以下输出:

请注意,无论grpA中的组数如何,此方法都有效.

Please note that this works regardless of the number of groups within grpA.

这篇关于在ggplot2中使用facet_grid()函数时,如何使用labeller()函数获取列总计显示在构面的标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆