Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ggplot和dplyr以及列名称作为字符串_R_String_Ggplot2_Dplyr - Fatal编程技术网

ggplot和dplyr以及列名称作为字符串

ggplot和dplyr以及列名称作为字符串,r,string,ggplot2,dplyr,R,String,Ggplot2,Dplyr,我想通过dplyr和ggplot使用字符串形式的列名来处理数据帧。这是我的密码 library(ggplot2) library(dplyr) my_df <- data.frame(var_1 = sample(c('a', 'b', 'c'), 1000, replace = TRUE), var_2 = sample(c('d', 'e', 'f'), 1000, replace = TRUE)) name_list = c('var_1',

我想通过
dplyr
ggplot
使用字符串形式的列名来处理数据帧。这是我的密码

library(ggplot2)
library(dplyr)
my_df <- data.frame(var_1 = sample(c('a', 'b', 'c'), 1000, replace = TRUE),
                    var_2 = sample(c('d', 'e', 'f'), 1000, replace = TRUE))

name_list = c('var_1', 'var_2')

for(el in name_list){
  pdf(paste(el, '.pdf', sep =''))
    test <- my_df %>% group_by(el) %>% summarize(count = n())
    ggplot(data = test, aes(x = el, y = count)) + geom_bar(stat='identity')
  dev.off()
}
上面的代码生成空文件

for(el in name_list){
  pdf(paste(el, '.pdf', sep =''))
    test <- my_df %>% group_by(UQ(el)) %>% summarize(count = n())
    ggplot(data = test, aes_string(x = el, y = 'count')) + geom_bar(stat='identity')
  dev.off()
}

我对您的代码做了两个更改:

  • 要在
    dplyr
    中使用“分组依据”变量,请使用
    group_by_
    而不是
    group_by
  • 要在
    ggplot2
    中调用变量,请使用
    aes_string
    get(variable)
    我还添加了一些小更改(例如,
    ggsave
    以保存绘图)

    库(ggplot2)
    图书馆(dplyr)
    我的_df%
    汇总(计数=n())%>%
    ggplot(aes(x=获取(el),y=计数))+
    几何图形栏(stat=“identity”)
    ggsave(粘贴0(el,.pdf),p)
    }
    
    您需要
    UQ
    (或
    !!
    )名称/符号。比如说

    for(el in name_list){
      pdf(paste(el, '.pdf', sep =''))
      test <- my_df %>% group_by(UQ(as.name(el))) %>% summarize(count = n())
      print(ggplot(data = test, aes_string(x = el, y = 'count')) + geom_bar(stat='identity'))
      dev.off()
    }
    
    for(名称列表中的el){
    pdf(粘贴(el,'.pdf',sep='')
    测试%groupby(UQ(as.name(el)))%>%summary(count=n())
    打印(ggplot(数据=测试,aes_字符串(x=el,y=count'))+geom_条(stat=identity'))
    发展主任()
    }
    
    Related:(dplyr)(ggplot)用
    UQ
    aes\u字符串显示您的尝试。给出您收到的错误消息。否则,这个问题的每一部分都只是一个简单问题的重复。
    ggsave
    起了很大的作用,但是没有
    ggsave
    它就不起作用了。您需要使用此方法在循环中打印()ggplot对象。我没有考虑那个问题,因为我错过了那个部分。
    for(el in name_list){
      pdf(paste(el, '.pdf', sep =''))
        test <- my_df %>% group_by(as.name(el)) %>% summarize(count = n())
        ggplot(data = test, aes_string(x = el, y = 'count')) + geom_bar(stat='identity')
      dev.off()
    }
    
    Error in mutate_impl(.data, dots) : 
      Column `as.name(el)` is of unsupported type symbol
    
    library(ggplot2)
    library(dplyr)
    my_df <- data.frame(var_1 = sample(c('a', 'b', 'c'), 1000, replace = TRUE),
                        var_2 = sample(c('d', 'e', 'f'), 1000, replace = TRUE))
    
    name_list = c('var_1', 'var_2')
    
    for(el in name_list){
        p <- my_df %>% 
             group_by_(el) %>% 
             summarize(count = n()) %>%
             ggplot(aes(x = get(el), y = count)) +
                 geom_bar(stat = "identity")
        ggsave(paste0(el, ".pdf"), p)
    }
    
    for(el in name_list){
      pdf(paste(el, '.pdf', sep =''))
      test <- my_df %>% group_by(UQ(as.name(el))) %>% summarize(count = n())
      print(ggplot(data = test, aes_string(x = el, y = 'count')) + geom_bar(stat='identity'))
      dev.off()
    }