【问题标题】:R Passing arguments for ggplot through a function call with facet_wrap and data subsettingR通过带有facet_wrap和数据子集的函数调用为ggplot传递参数
【发布时间】:2020-04-01 22:51:11
【问题描述】:

我有一个纵向数据,我通过 facet_wrap 和数据框的子集传递给 ggplot。我希望“发挥作用”——实现这一点,但我遇到了麻烦。我看过与此类似的帖子,但没有在函数内部包含 facet_wrap 和数据子集的帖子。例如,我过去曾使用来自this post 的信息来做简单的图表。下面我展示了用于生成虚拟数据然后绘制图形的代码部分。这行得通。当我尝试使用函数调用时,我收到错误消息:

非常感谢您的帮助。谢谢!!

错误:分面变量必须至少有一个值

# Test

# Generate data
DF <- expand.grid(Time=(0:10), variable=as.factor(c("ux0", "ux1", "ux2")), model=as.factor(c("Model 1", "Model 2", "Model 3")))
DF$value <- DF$Time + rnorm(nrow(DF), 0, 20)

# load libraries
library(ggplot2)
library(scales)

# Define themes
My_Theme = theme_bw()+
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        aspect.ratio = 1,
        axis.title=element_text(size=7),
        axis.text.x=element_text(size=rel(0.6)),
        axis.text.y=element_text(size=rel(0.6)),
        strip.text = element_text(size = 6))

#Plot
my.plot  =  
  ggplot(subset(DF, variable %in% "ux1")) +
  geom_line(aes(x=Time, y=value)) +
  facet_wrap( ~ model, ncol=3) + 
  labs(x = "Time [s]", y = expression(paste("U"[X],","[1]))) +
  My_Theme
print(my.plot)

#Now try with function
makePlots <- function(data, subSetVar, subSetval, xVar, yVar, facetVar, 
                      xLabel, yLabel){

  # Common Theme to all plots

  My_Theme = theme_bw()+
    theme(panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          aspect.ratio = 1,
          axis.title=element_text(size=7),
          axis.text.x=element_text(size=rel(0.6)),
          axis.text.y=element_text(size=rel(0.6)),
          strip.text = element_text(size = 6))

  my.plot  =  
    ggplot(subset(data, subSetVar %in% subSetval)) + 
    geom_line(aes(x=xVar, y=yVar)) + 
    facet_wrap(facetVar, ncol=3) + 
    labs(x = xLabel, y = yLabel) +
    My_Theme


  # Output to Plots window in RStudio
  print(my.plot)

}

my.plot <- makePlots(DF, "variable", "ux1", "Time", "value", "model",
                     "Time [s]", expression(paste("U"[X],","[1])))

'''

【问题讨论】:

    标签: r ggplot2 subset facet-wrap


    【解决方案1】:

    为了将字符串作为变量传递给ggplot,您需要在将my.plot 部分包装成函数之前对其进行一些更改。

    对于数据集的子集,您需要使用[[ ]] 传递列的名称才能使其工作。对于xy的定义,可以使用aes_stringhttps://ggplot2.tidyverse.org/reference/aes_.html)。最后,对于分面,将您的字符向量作为公式传递(如本文所述:Passing string variable facet_wrap() in ggplot using R)。

    my.plot  =  
        ggplot(subset(data, data[[subSetVar]] %in% subSetval)) + 
        geom_line(aes_string(x=xVar, y=yVar)) +
        facet_wrap(as.formula(paste("~", facetVar)), ncol=3) + 
        labs(x = xLabel, y = yLabel) +
        My_Theme
    

    然后,它应该可以工作并为您提供相同的图表:

    my.plot <- makePlots(DF, "variable", "ux1", "Time", "value", "model",
                         "Time [s]", expression(paste("U"[X],","[1])))
    
    

    它回答了你的问题吗?

    【讨论】:

    • 非常感谢。效果很好!
    猜你喜欢
    • 1970-01-01
    • 2012-08-06
    • 2018-11-19
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多