R语言 使用拼接创建一个带面网格

nwsw7zdq  于 2024-01-03  发布在  其他
关注(0)|答案(2)|浏览(252)

我使用的specific function在网格搜索算法中返回ggplot,我想在绘图网格中排列结果图,就像facet_grid()一样。
我不能使用标准的方面,因为每个场景只能在特定的尺度下解释,所以每个图都有独立的x/y限制。
下面是一个可重复的例子:

  1. library(tidyverse)
  2. library(patchwork)
  3. get_plot = function(a, b) list(ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  4. geom_point() + ggtitle(paste0(a, "--", b)))
  5. x = expand_grid(dist_fun = c("rnorm", "rexp"),
  6. assumption=c("linear", "square")) %>%
  7. rowwise() %>%
  8. mutate(plot=get_plot(dist_fun, assumption))
  9. x
  10. #> # A tibble: 4 × 3
  11. #> # Rowwise:
  12. #> dist_fun assumption plot
  13. #> <chr> <chr> <list>
  14. #> 1 rnorm linear <gg>
  15. #> 2 rnorm square <gg>
  16. #> 3 rexp linear <gg>
  17. #> 4 rexp square <gg>
  18. #expected outcome, missing strips:
  19. wrap_plots(x$plot)

字符串
x1c 0d1x的数据
创建于2023-12-08带有reprex v2.0.2
有没有办法把ggtitle的标识替换成刻面状的条?

92dk7w1h

92dk7w1h1#

好吧,这不是关于使用哪个函数的问题。要使用patchwork获得类似facet_grid的外观,需要您编写一个自定义函数,在将ggplot对象传递给wrap_plots之前对其进行操作。为此,我首先添加了一个包含补丁中数字位置的列。然后,使用自定义函数,您可以删除轴,并根据位置添加条带:
注意事项:虽然我喜欢这样的patchwork练习,但恕我直言,更简单的方法是从你的函数或包含数据和图的列表中返回数据,然后使用rbind/bind_rows + facet_grid,就像评论中已经建议的那样。

  1. library(tidyverse)
  2. library(patchwork)
  3. get_plot <- function(a, b) {
  4. list(ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  5. geom_point())
  6. }
  7. x <- expand_grid(
  8. dist_fun = c("rnorm", "rexp"),
  9. assumption = c("linear", "square")
  10. ) %>%
  11. mutate(plot = get_plot(dist_fun, assumption))
  12. x |>
  13. mutate(pos = row_number()) |>
  14. pmap(
  15. \(...) {
  16. args <- list(...)
  17. col <- 1 + (args$pos + 1) %% 2
  18. row <- 1 + (args$pos - 1) %/% 2
  19. remove_x <- if (row == 1) {
  20. theme(
  21. axis.text.x = element_blank(),
  22. axis.ticks.x = element_blank(),
  23. axis.ticks.length.x = unit(0, "pt")
  24. )
  25. }
  26. remove_y <- if (col == 2) {
  27. theme(
  28. axis.text.y = element_blank(),
  29. axis.ticks.y = element_blank(),
  30. axis.ticks.length.y = unit(0, "pt")
  31. )
  32. }
  33. facet_y <- if (row == 1) {
  34. # Wrap in quotes to get e.g.
  35. # "linear" ~ . instead of linear ~ .
  36. paste0("\"", args$assumption, "\"")
  37. } else {
  38. "."
  39. }
  40. facet_x <- if (col == 2) {
  41. paste0("\"", args$dist_fun, "\"")
  42. } else {
  43. "."
  44. }
  45. layer_facet <- facet_grid(reformulate(facet_y, facet_x))
  46. args$plot +
  47. remove_x +
  48. remove_y +
  49. layer_facet
  50. }
  51. ) |>
  52. wrap_plots(ncol = 2) &
  53. theme(
  54. axis.title = element_blank()
  55. )

字符串
x1c 0d1x的数据

展开查看全部
dl5txlt9

dl5txlt92#

正如其他人所说,最简单的方法是使用x对象,去掉数据,在基本的图结构中添加面。例如,下面的函数:

  1. facetize <- function(x) {
  2. base_plot <- x$plot[[1]]
  3. x_nm <- names(x)
  4. base_plot$data <- x %>%
  5. mutate(plot_data = map(plot, `[[`, 'data')) %>%
  6. select(-plot) %>%
  7. unnest(plot_data)
  8. base_plot +
  9. facet_grid(rows = vars(!!str2lang(x_nm[1])),
  10. cols = vars(!!str2lang(x_nm[2])),
  11. scales = 'free')
  12. }

字符串
允许:

  1. facetize(x)


的数据

展开查看全部

相关问题