函数中的dplyr和ggplot:在es函数中使用重新排序

Ben

我正在努力重新排序我的数据,以便在也使用dplyr的函数中使用ggplot进行绘图:

# example data
library(ggplot2)
library(dplyr)
dat <- data.frame(a = c(rep("l", 10), rep("m", 5), rep("o", 15)),
                  b = sample(100, 30), 
                  c= c(rep("q", 10), rep("r", 5), rep("s", 15)))

这是我在函数之外的步骤:

# set a variable
colm <- "a"
# make a table
dat1 <- dat %>% 
  group_by_(colm) %>%  
  tally(sort = TRUE)
# put in order and plot
ggplot(dat2, aes(x = reorder(a, n), y = n)) +
  geom_bar(stat = "identity")

在此处输入图片说明

但是,当我尝试将其变成函数时,我似乎无法使用reorder

f <-  function(the_data, the_column){
       dat %>% group_by_(the_column) %>%  
       tally(sort = TRUE) %>% 
       ggplot(aes_string(x = reorder(the_column, 'n'), y = 'n')) +
       geom_bar(stat = "identity")
}

f(dat, "a")

Warning message:
In mean.default(X[[i]], ...) :
  argument is not numeric or logical: returning NA

在此处输入图片说明

该功能将在没有reorder以下条件的情况下运行

f <-  function(the_data, the_column){
       dat %>% group_by_(the_column) %>%  
       tally(sort = TRUE) %>% 
       ggplot(aes_string(x = the_column, y = 'n')) +
       geom_bar(stat = "identity")
}

f(dat, "a")

在此处输入图片说明

如果没有dplyr,我可以得到我想要的东西,但是我更喜欢使用dplyr,因为它在我的实际用例中更有效:

# without dplyr
ff = function(the_data, the_column) {
  data.frame(table(the_data[the_column])) %>% 
  ggplot(aes(x = reorder(Var1, Freq), y = Freq)) +
  geom_bar(stat = "identity") +
    ylab("n") +
    xlab(the_column)
}

ff(dat, "a")

在此处输入图片说明

我看到别人有这个(挣扎12),但它似乎必须有这个重新排序-IN-A-功能任务更高效dplyr /管成语。

弗里克先生

如果要使用aes_string,则整个值必须是一个字符串,而不仅仅是部分字符串。您可以paste()用来帮助构建要用于的表达式x例如

f <-  function(the_data, the_column){
       dat %>% group_by_(the_column) %>%  
       tally(sort = TRUE) %>% 
       ggplot(aes_string(x = paste0("reorder(",the_column,", n)"), y = 'n')) +
       geom_bar(stat = "identity")
}

或者您可以使用表达式而不是字符串

f <-  function(the_data, the_column){
       dat %>% group_by_(the_column) %>%  
       tally(sort = TRUE) %>% 
       ggplot(aes_q(x = substitute(reorder(x, n),list(x=as.name(the_column))), y = quote(n))) +
       geom_bar(stat = "identity")
}

但是一般的想法是,在混合字符串和原始语言元素(例如名称或表达式)时,需要小心。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在包装在函数中的ggplot2中使用重新排序

在dplyr中编码的函数中使用函数

R-使用ggplot2在函数中对条形图进行重新排序

使用递归函数重新排序

如何在带有tidyr和ggplot2的函数中使用dplyr的enquo和quo_name

带有 dplyr 和 ggplot2 的函数中的 Tidyeval

如何使用ggplot和dplyr从函数中的量化创建因子变量?

在dplyr中使用跨函数

R中使用purrr和dplyr(列表列工作流)的函数中的if语句

构造函数和指令重新排序

ggplot子集数据函数和dplyr

在dplyr中的函数中使用字符串

使用dplyr和mapply函数时,将R中的ggplot2输出保存到pdf文件中

在ggplot2中的aes()函数中使用颜色

如何在R中使用list.files函数对存储进行重新排序

在R中:将列名称作为参数传递,并在dplyr :: mutate()和lazyeval :: interp()函数中使用它

在 dplyr 的 mutate 函数中使用列名

在函数中使用dplyr的问题(group_by)

在函数中使用dplyr group_by

在R中的函数中使用dplyr,然后在for循环中执行该函数

Swift:如何重新排序和省略存储在变量中的函数中的参数

在C ++ 11中使用std :: sort和lambda函数对动态分配的多维C数组进行排序

ES6箭头函数中使用“()”和“ _”的区别

在排序宏中使用VBA函数

如何在排序函数中使用rbegin()?

在函数中使用%>%和ifelse()

在 R 中您自己的包中使用 dplyr 函数时关闭 dplyr 消息

在dplyr的summary函数中使用过滤器函数

重新排序无法在ggplot中使用我当前的数据帧