R 函数表示找不到列

R 函数表示找不到列,r,function,ggplot2,dplyr,R,Function,Ggplot2,Dplyr,我在R中使用这个函数来生成条形图,但每次我在数据集上尝试它时,它都会给我一个错误,说找不到columname。你能帮我找到臭虫在哪里吗 提前感谢您帮助解决此问题 代码如下: plot_dist <- function(data, column_name, title = "Please provide a title") { ## This function computes a barplot of a given column_name for a given datase

我在R中使用这个函数来生成条形图,但每次我在数据集上尝试它时,它都会给我一个错误,说找不到columname。你能帮我找到臭虫在哪里吗

提前感谢您帮助解决此问题

代码如下:

plot_dist <-
  function(data, column_name, title = "Please provide a title") {
    ## This function computes a barplot of a given column_name for a given dataset with the title
    data %>%
      group_by(column_name) %>%
      summarise(Nombre = n()) %>%
      ggplot(aes(column_name, Nombre)) +
      geom_bar(stat = "identity", fill = 'steelblue') +
      geom_text(
        aes(label = Nombre),
        vjust = -0.5,
        color = 'steelblue',
        size = 3.5
      ) +
      theme_minimal() +
      ggtitle(title) +
      theme(plot.title = element_text(hjust = 0.5))
}
返回

错误:列
列\u名称
未知


我认为最简单的方法是:


查找
ggplot2
和非标准评估(NSE)。试试这个
plot_dist(pps, gender, "Distribution de genre") 
...
group_by({{ column_name}} ) %>%
      summarise(Nombre = n()) %>%
      ggplot(aes({{ column_name }}, Nombre)) +
...

plot_dist(mtcars, cyl, "Distribution de cilindros")