【问题标题】:Show plot according to argument with patchwork package根据带有拼凑包的参数显示情节
【发布时间】:2020-06-08 03:00:51
【问题描述】:

我正在尝试使用 patchwork 包编写一个函数,其中根据函数中传递的参数显示图。我尝试了下面的方法,将未在函数中传递的对象设置为 NULL。但是,它仅在第一个对象不同于 NULL 时才有效。有什么想法吗?

# 1. PACKAGES

library(tidyverse)
library(patchwork)

# 2. DATA

data = starwars

# 3. FUNCTION

plot_people = function (homeworld = c("Tatooine", "Naboo", "Alderaan")) {

  p1 = if (is.element("Tatooine", homeworld)) {

    data %>%
    filter(homeworld == "Tatooine") %>%
    ggplot(aes(x = mass, y = height,
               label = ifelse(species == "Human", name, NA))) +
    geom_point() +
    geom_label()

  } else {

    NULL
  }

  p2 = if (is.element("Naboo", homeworld)) {

    data %>%
      filter(homeworld == "Naboo") %>%
      ggplot(aes(x = mass, y = height,
                 label = ifelse(species == "Human", name, NA))) +
      geom_point() +
      geom_label()

  } else {

    NULL
  }

  p3 = if (is.element("Alderaan", homeworld)) {

    data %>%
      filter(homeworld == "Alderaan") %>%
      ggplot(aes(x = mass, y = height,
                 label = ifelse(species == "Human", name, NA))) +
      geom_point() +
      geom_label()

  } else {

    NULL
  }

  # how to write this line in order to plot only objects in homeworld argument?
  p1 + p2 + p3

}

# 4. RESULTS

plot_people(homeworld = c("Naboo", "Tatooine"))

plot_people(homeworld = c("Naboo", "Alderaan"))
#> NULL

reprex package (v0.3.0) 于 2020-06-07 创建

【问题讨论】:

    标签: r ggplot2 tidyverse patchwork


    【解决方案1】:

    由于您需要为homeworld 的每个元素运行相同的代码,因此您可以使用purrr::map(或lapply,如果您愿意)对其进行迭代。这将返回一个包含每次迭代的元素的列表,这里包含一个图(如p1p2 等)。这个列表可以是reduced(或Reduced)来迭代组合每个元素与+

    library(tidyverse)
    library(patchwork)
    
    plot_people = function (homeworld = c("Tatooine", "Naboo", "Alderaan")) {
    
        plots <- map(homeworld, function(hw){
            starwars %>%
                filter(homeworld == hw) %>%
                ggplot(aes(x = mass, y = height,
                           label = ifelse(species == "Human", name, NA))) +
                geom_point() +
                geom_label()
        })
    
        reduce(plots, `+`)
    }
    
    plot_people(homeworld = c("Naboo", "Tatooine"))
    

    plot_people(homeworld = c("Naboo", "Alderaan"))
    

    或者,您可以编写wrap_plots(plots),而不是reduce(plots, `+`),使用patchwork 中的wrap_plots() 函数,该函数接受绘图列表。结果是一样的。

    更一般地说,您应该在拼凑之前考虑刻面:

    library(tidyverse)
    
    plot_people = function (homeworld = c("Tatooine", "Naboo", "Alderaan")) {
        starwars %>%
            filter(homeworld %in% !!homeworld) %>%
            ggplot(aes(x = mass, y = height,
                       label = ifelse(species == "Human", name, NA))) +
            geom_point() +
            geom_label() + 
            facet_wrap(~homeworld)
    }
    
    plot_people(homeworld = c("Naboo", "Tatooine"))
    

    plot_people(homeworld = c("Naboo", "Alderaan"))
    

    请注意,您可以通过这种方法免费获得漂亮的面板条标签,识别哪个星球是哪个星球。

    【讨论】:

    • 太棒了,谢谢(再次)!我会尝试两种方法!
    猜你喜欢
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2017-11-07
    相关资源
    最近更新 更多