使用ggplot绘制data.frame列

时间:2018-03-17 11:33:04

标签: r ggplot2 markdown r-markdown

我需要(1)生成一个markdown文件,其中包含(2)一系列图表,用于(3)匹配特定字符串的数据集的特定列

此刻我陷入了第二点。 我想使用ggplot为名称以" pre _"开头的列绘制一些有序字符因子。 这是我到目前为止所使用的代码

#load ggplot
library(ggplot2)
#reproduce a generic dataset
level=c("Strongly Agree", "Agree", "Neither agree or disagree","Disagree", "Strongly disagree",NA)
df <- data.frame(pre_1=as.character(sample(level, 20, replace = T)), 
                 pre_2=as.character(sample(level, 20, replace = T)),
                 post_1=as.character(sample(level, 20, replace = T)),
                 post_2=as.character(sample(level, 20, replace = T)),
                 stringsAsFactors=T)

   ## function to plot each colum of the dataset that starts with pre_
dfplot_pre <- function(x)
{
  df <- x
  ln <- length(names(dplyr::select(df, starts_with("pre_"))))
  for(i in 1:ln){
    out <- lapply(df , function(x) factor(x, c("Strongly Agree", "Agree", "Neither agree or disagree","Disagree", "Strongly disagree"),ordered = T ))
    df <- do.call(data.frame , out )
    if(is.factor(df[,i])){ggplot(na.omit(data.frame(df[,i],stringsAsFactors = T)), aes(x=na.omit(df[i]))) +
        theme_bw() +  
        geom_bar(aes(y = (..count..)/sum(..count..))) + 
        scale_y_continuous(labels=percent) + 
        scale_x_discrete(drop=FALSE)}
    else{print (fail)}
  }
}

不幸的是,代码没有显示任何情节。我能够使用命令

正确绘制列
plot(df[,i])

所以我怀疑它在ggplot中出了点问题,但不确定是什么。 在函数外部运行代码并将其存储在对象中会出现此错误:

Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 0, 1

非常感谢

2 个答案:

答案 0 :(得分:1)

原始解决方案:

plot_pre <- function(df){
  select(data, starts_with("pre_")) %>% 
    length() %>% 
    seq(1,.,1)  %>% 
    for (i in .){
      if (dummy(as.character(select(data, starts_with("pre_"))[[i]])) == TRUE) {
        data.frame(select(data, starts_with("pre_"))[[i]])  %>%
          na.omit() %>%
          ggplot(.,aes(x=.))  +  
          geom_bar(aes(y = (..count..)/sum(..count..)), stat="count") + 
          geom_text(aes( label =paste(round((..count..)/sum(..count..)*100),"%"), y= (..count..)/sum(..count..)), stat= "count", vjust = -.5)+
          scale_y_continuous(labels=percent,limits = c(-0, 1)) + 
          scale_x_discrete(drop=FALSE) + 
          ylab("Relative Frequencies (%)") + 
          ggtitle(names(select(data, starts_with("pre_")))[i]) +
          theme_light(base_size = 18) +
          theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
          theme(plot.title = element_text(hjust = 0.5)) -> dummyplot
        print(dummyplot)}
      else {
        factor(select(data, starts_with("pre_"))[[i]], c("Strongly Agree", "Agree", "Neither agree or disagree","Disagree", "Strongly disagree"),ordered = T ) %>% 
          data.frame() %>%
          na.omit() %>%
          ggplot(.,aes(x=.))  +  
          geom_bar(aes(y = (..count..)/sum(..count..)), stat="count") + 
          geom_text(aes( label =paste(round((..count..)/sum(..count..)*100),"%"), y= (..count..)/sum(..count..)), stat= "count", vjust = -.5)+
          scale_y_continuous(labels=percent,limits = c(-0, 1)) + 
          scale_x_discrete(drop=FALSE) + 
          ylab("Relative Frequencies (%)")+
          ggtitle(names(select(data, starts_with("pre_")))[i]) +
          theme_light(base_size = 18) +
          theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
          theme(plot.title = element_text(hjust = 0.5))-> contplot
        print(contplot)
      }}  
}

dfplot_pre(df)

答案 1 :(得分:0)

替代解决方案

library(tidyverse)
library(scales)

dfplot_pre <- function(df)  {
   select(df, starts_with("pre_")) %>% 
      na.omit() %>% 
      gather() %>% 
      mutate(value = factor(value, levels = c("Strongly Agree", "Agree", "Neither agree or disagree","Disagree", "Strongly disagree"), ordered = TRUE)) %>% 
      ggplot(aes(x = value))  +  
      geom_bar(aes(y = (..count..)/sum(..count..))) + 
      scale_y_continuous(labels=percent) + 
      scale_x_discrete(drop=FALSE) +  
      facet_wrap(~ key) +
      theme_bw() +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
 }