【问题标题】:How to subset data within function using rlang verbs?如何使用 rlang 动词对函数内的数据进行子集化?
【发布时间】:2021-07-08 00:17:46
【问题描述】:

我正在尝试循环几个图并将它们组合成一个cowplot::plot_grid。我无法将绘图列表直接提供给 plot_grid 函数,因为我正在自定义绘图以便每行只显示一个 y 轴。

library(tidyverse)
library(sjPlot)
library(rlang)
data(mtcars)
model <- lm(hp ~ cyl + mpg, data = mtcars)
plotlist <- map(c("cyl", "mpg"), function(x) plot_model(model, type = "pred", terms = {{x}}) )
combine_plots <- function(plotlist) {
  plot_grid(plotlist[[1]],   
            plotlist[[2]] + 
              theme(axis.text.y = element_blank(),
                    axis.ticks.y = element_blank(),
                    axis.title.y = element_blank() ))
}
map(plotlist, function(x) combine_plots(enquo(x)))

不幸的是,我收到了错误 Error in x[["plot.list"]] : object of type 'symbol' is not subsettable。我认为我使用了错误的 quasiquotation 动词。我该如何纠正错误?

【问题讨论】:

  • 一般情况下,您只需将enquo(x) 替换为x — 这里不需要非标准评估。但实际上,整个最后一行需要替换为combine_plots(plotlist)

标签: r rlang


【解决方案1】:

我对@9​​87654323@ 的情节布局不太熟悉,但这可以在patchwork 中完成:

combine_plots <- function(plotlist) {
  patchwork::plot_layout(
    plotlist[[1]] |
    plotlist[[2]] + 
      theme(axis.text.y = element_blank(),
            axis.ticks.y = element_blank(), 
            axis.title.y = element_blank() ))
}

combine_plots(plotlist)

(我保留了不对齐 y 轴的 OP 代码,但您可能希望明确 y 轴,以便两个图对齐,例如 coord_cartesian(ylim = c(0,250))

【讨论】:

    猜你喜欢
    • 2017-11-21
    • 1970-01-01
    • 2023-03-15
    • 2015-12-28
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多