ggplot报告连续变量是绝对变量;我想念什么?

时间:2018-11-08 11:50:28

标签: r ggplot2

我正在尝试遍历数据帧,并根据列是因子还是数字返回多面ggplot。

library(tidyverse)
d <- mtcars 
# make a couple factors
d2 <- d %>% mutate_at((vars(matches("cyl"),matches("gear"))), as.factor)
#check
(d2 %>% map_lgl(is.factor))
(d2 %>% map_lgl(is.numeric))

这有效:

plot_chart <- function(df, na.rm = TRUE){
  nm = names(df)
  for (i in nm) {
    nm_p <- grep(paste("^",i,"$",sep=""), nm)  #finds the position of i in the sequence
    g <- ggplot(df, aes_string(x = i))
    print(nm_p) # progress to console
    if (is.factor(df[,nm_p])==TRUE) {
        g <- g + geom_bar()
      } else if (is.numeric(df[,nm_p])) {
        g <- g + geom_histogram(bins = 10)
      }
    g <- g + facet_wrap(~cyl,nrow=1) + labs( x = i)
    print(g) 

    # indexes and saves
    # idx <- paste(sprintf("%04d", nm_p), "_Cyl_by_", i,".svg", sep="")
    # ggsave(idx , device="svg", width = 10, height = 10, units = "cm")
  }
}

plot_chart(d2)  #Success!

但是,尝试进一步抽象该功能(最终目标是添加其他图表类型并优化输入)会失败:

callplot <- function(df, na.rm = TRUE){
  nm = names(df)
  for (i in nm) {
    #finds the position of i in the sequence
    nm_p <- grep(paste("^",i,"$",sep=""), nm)
    # print(nm_p) # progress to console
    if (is.factor(df[,nm_p])==TRUE) {
      t <- "fac"
    } else if (is.numeric(df[,nm_p])) {
      t <- "num"
    }
    plotme(df, i, t)
  }
}

plotme <- function(df, x, type, na.rm = TRUE){
  xq <- enquo(x)
  g <- ggplot(df, aes(x = !! xq))
  if (type=="fac") {
    g <- g + geom_bar()
  } else if (type=="num") {
    print(x)   
    g <- g + geom_histogram(bins=10) #<--- fails here 
  }
  g +  facet_wrap(~cyl,nrow=1) + labs( x = x)
  print(g)
}

callplot(d2)

错误是:

  

错误:StatBin需要连续的x变量:x变量是离散的。也许您想要stat =“ count”?

但是,如逻辑所示,mpg不是一个因素。这与enquo!!有关吗?这些对我来说是一个全新的困惑。

我想念什么?

0 个答案:

没有答案