如何在facet_grid中删除特定列?

huangapple go评论122阅读模式
英文:

How to get rid of a certain column in facet_grid?

问题

以下是代码的翻译部分:

  1. 我有以下示例代码:
  2. tmp <- data.frame(
  3. "channel_topic" = rep(LETTERS[seq(1:16)],9),
  4. "p1_age_cat_complete" = rep(1:9, each = 16),
  5. "n" = sample(100, size = 144, replace = TRUE)) |>
  6. group_by(channel_topic) |>
  7. mutate(n_group = sum(n))|>
  8. group_by(n_group) |>
  9. mutate(group_id = abs(cur_group_id()-17)) |>
  10. ungroup() |>
  11. mutate(group_id = ifelse(str_detect(channel_topic, "N"),
  12. 99,
  13. ifelse(str_detect(channel_topic, "O"),
  14. 100,
  15. ifelse(str_detect(channel_topic, "P"),
  16. 101,
  17. group_id)))) |>
  18. mutate(facets = ifelse(group_id < 99,"1","2"),
  19. facets_2 = ifelse(p1_age_cat_complete == "9", "2","1"))
  20. ggplot(tmp) +
  21. facet_grid(
  22. cols = vars(facets_2),
  23. rows = vars(facets),
  24. scales = "free_y",
  25. space = "free_y",
  26. ) +
  27. geom_tile(
  28. aes(
  29. x = p1_age_cat_complete,
  30. y = reorder(channel_topic, -group_id),
  31. fill = n
  32. )
  33. ) + theme(
  34. axis.text.x = element_text(angle = 90)
  35. ) +
  36. labs(
  37. x = "Alter",
  38. y = "Channel Topic",
  39. fill = "Anzahl",
  40. subtitle = paste("N = ", sum(tmp$n), sep = "")
  41. ) + theme(
  42. panel.background = element_rect(fill = "white",
  43. colour = "white"),
  44. panel.grid.major = element_line(colour = "gray90"),
  45. panel.grid.minor = element_line(colour = "grey95"),
  46. plot.subtitle = element_text(hjust = 1),
  47. axis.text.x = element_text(angle = 90),
  48. strip.background = element_blank(),
  49. strip.text = element_blank()
  50. ) +
  51. scale_x_discrete(limits = 1:9)

输出如下所示:

如何在facet_grid中删除特定列?

如何去除绘图左侧的 9 列和右侧除 9 外的所有列。基本上,我想去除空列,并只保留y轴上的一小部分空间。

提前感谢。

最好,

Aaron

  1. <details>
  2. <summary>英文:</summary>
  3. I have the following example code:

tmp <- data.frame(
"channel_topic" = rep(LETTERS[seq(1:16)],9),
"p1_age_cat_complete" = rep(1:9, each = 16),
"n" = sample(100, size = 144, replace = TRUE)) |>
group_by(channel_topic) |>
mutate(n_group = sum(n))|>
group_by(n_group) |>
mutate(group_id = abs(cur_group_id()-17)) |>
ungroup() |>
mutate(group_id = ifelse(str_detect(channel_topic, "N"),
99,
ifelse(str_detect(channel_topic, "O"),
100,
ifelse(str_detect(channel_topic, "P"),
101,
group_id)))) |>
mutate(facets = ifelse(group_id < 99,"1","2"),
facets_2 = ifelse(p1_age_cat_complete == "9", "2","1"))

ggplot(tmp) +
facet_grid(
cols = vars(facets_2),
rows = vars(facets),
scales = "free_y",
space = "free_y",
) +
geom_tile(
aes(
x = p1_age_cat_complete,
y = reorder(channel_topic, -group_id),
fill = n
)
) + theme(
axis.text.x = element_text(angle = 90)
) +
labs(
x = "Alter",
y = "Channel Topic",
fill = "Anzahl",
subtitle = paste("N = ", sum(tmp$n), sep = "")
) + theme(
panel.background = element_rect(fill = "white",
colour = "white"),
panel.grid.major = element_line(colour = "gray90"),
panel.grid.minor = element_line(colour = "grey95"),
plot.subtitle = element_text(hjust = 1),
axis.text.x = element_text(angle = 90),
strip.background = element_blank(),
strip.text = element_blank()
) +
scale_x_discrete(limits = 1:9)

  1. The output looks like this:
  2. [![enter image description here][1]][1]
  3. How can I get rid of the *9* column on the left side of the plot and of all other column exept *9* on the right side of the plot. Basically I want to suppress the empty columns and have only a little space like on the y-axis.
  4. Thank you in advance.
  5. Best,
  6. Aaron
  7. [1]: https://i.stack.imgur.com/Iu9Fx.png
  8. [2]: https://i.stack.imgur.com/aJSTs.png
  9. </details>
  10. # 答案1
  11. **得分**: 1
  12. 这是您正在寻找的吗?
  13. 1. `p1_age_cat_complete` 被转化为一个因子
  14. 2. x轴设置了`scales``space`的自由度,除了y
  15. 3. 删除了 `scale_x_discrete`
  16. _edit_ 我忘记注明我在 `labs()` 之后删除了第一个 `theme(axis.text.x...` 的调用,因为它在那之后重复了。
  17. ``` r
  18. library(tidyverse)
  19. tmp &lt;- data.frame(
  20. &quot;channel_topic&quot; = rep(LETTERS[seq(1:16)],9),
  21. &quot;p1_age_cat_complete&quot; = rep(1:9, each = 16),
  22. &quot;n&quot; = sample(100, size = 144, replace = TRUE)) |&gt;
  23. group_by(channel_topic) |&gt;
  24. mutate(n_group = sum(n))|&gt;
  25. group_by(n_group) |&gt;
  26. mutate(group_id = abs(cur_group_id()-17)) |&gt;
  27. ungroup() |&gt;
  28. mutate(group_id = ifelse(str_detect(channel_topic, &quot;N&quot;),
  29. 99,
  30. ifelse(str_detect(channel_topic, &quot;O&quot;),
  31. 100,
  32. ifelse(str_detect(channel_topic, &quot;P&quot;),
  33. 101,
  34. group_id)))) |&gt;
  35. mutate(facets = ifelse(group_id &lt; 99,&quot;1&quot;,&quot;2&quot;),
  36. facets_2 = ifelse(p1_age_cat_complete == &quot;9&quot;, &quot;2&quot;,&quot;1&quot;),
  37. p1_age_cat_complete = factor(p1_age_cat_complete))
  38. tmp %&gt;%
  39. ggplot() +
  40. facet_grid(
  41. cols = vars(facets_2),
  42. rows = vars(facets),
  43. scales = &quot;free&quot;,
  44. space = &quot;free&quot;
  45. ) +
  46. geom_tile(
  47. aes(
  48. x = p1_age_cat_complete,
  49. y = reorder(channel_topic, -group_id),
  50. fill = n
  51. )
  52. ) +
  53. labs(
  54. x = &quot;Alter&quot;,
  55. y = &quot;Channel Topic&quot;,
  56. fill = &quot;Anzahl&quot;,
  57. subtitle = paste(&quot;N = &quot;, sum(tmp$n), sep = &quot;&quot;)
  58. ) + theme(
  59. panel.background = element_rect(fill = &quot;white&quot;,
  60. colour = &quot;white&quot;),
  61. panel.grid.major = element_line(colour = &quot;gray90&quot;),
  62. panel.grid.minor = element_line(colour = &quot;grey95&quot;),
  63. plot.subtitle = element_text(hjust = 1),
  64. axis.text.x = element_text(angle = 90),
  65. strip.background = element_blank(),
  66. strip.text = element_blank()
  67. )

如何在facet_grid中删除特定列?<!-- -->

<sup>创建于2023年08月04日,使用 reprex v2.0.2</sup>

英文:

Is this what you're looking for?

  1. p1_age_cat_complete is made into a factor
  2. Set scales and space free for the x axis in addition to the y
  3. Removed scale_x_discrete

edit I forgot to note I removed the first theme(axis.text.x... call as its repeated after labs().

  1. library(tidyverse)
  2. tmp &lt;- data.frame(
  3. &quot;channel_topic&quot; = rep(LETTERS[seq(1:16)],9),
  4. &quot;p1_age_cat_complete&quot; = rep(1:9, each = 16),
  5. &quot;n&quot; = sample(100, size = 144, replace = TRUE)) |&gt;
  6. group_by(channel_topic) |&gt;
  7. mutate(n_group = sum(n))|&gt;
  8. group_by(n_group) |&gt;
  9. mutate(group_id = abs(cur_group_id()-17)) |&gt;
  10. ungroup() |&gt;
  11. mutate(group_id = ifelse(str_detect(channel_topic, &quot;N&quot;),
  12. 99,
  13. ifelse(str_detect(channel_topic, &quot;O&quot;),
  14. 100,
  15. ifelse(str_detect(channel_topic, &quot;P&quot;),
  16. 101,
  17. group_id)))) |&gt;
  18. mutate(facets = ifelse(group_id &lt; 99,&quot;1&quot;,&quot;2&quot;),
  19. facets_2 = ifelse(p1_age_cat_complete == &quot;9&quot;, &quot;2&quot;,&quot;1&quot;),
  20. p1_age_cat_complete = factor(p1_age_cat_complete))
  21. tmp %&gt;%
  22. ggplot() +
  23. facet_grid(
  24. cols = vars(facets_2),
  25. rows = vars(facets),
  26. scales = &quot;free&quot;,
  27. space = &quot;free&quot;
  28. ) +
  29. geom_tile(
  30. aes(
  31. x = p1_age_cat_complete,
  32. y = reorder(channel_topic, -group_id),
  33. fill = n
  34. )
  35. ) +
  36. labs(
  37. x = &quot;Alter&quot;,
  38. y = &quot;Channel Topic&quot;,
  39. fill = &quot;Anzahl&quot;,
  40. subtitle = paste(&quot;N = &quot;, sum(tmp$n), sep = &quot;&quot;)
  41. ) + theme(
  42. panel.background = element_rect(fill = &quot;white&quot;,
  43. colour = &quot;white&quot;),
  44. panel.grid.major = element_line(colour = &quot;gray90&quot;),
  45. panel.grid.minor = element_line(colour = &quot;grey95&quot;),
  46. plot.subtitle = element_text(hjust = 1),
  47. axis.text.x = element_text(angle = 90),
  48. strip.background = element_blank(),
  49. strip.text = element_blank()
  50. )

如何在facet_grid中删除特定列?<!-- -->

<sup>Created on 2023-08-04 with reprex v2.0.2</sup>

答案2

得分: 0

Sure, here is the translated code:

  1. p <- ggplot(tmp) +
  2. geom_tile(
  3. aes(
  4. x = p1_age_cat_complete,
  5. y = reorder(channel_topic, -group_id),
  6. fill = n
  7. )
  8. ) + theme(
  9. axis.text.x = element_text(angle = 90)
  10. ) +
  11. labs(
  12. x = "Alter",
  13. y = "Channel Topic",
  14. fill = "Anzahl",
  15. subtitle = paste("N = ", sum(tmp$n), sep = "")
  16. ) + theme(
  17. panel.background = element_rect(fill = "white",
  18. colour = "white"),
  19. panel.grid.major = element_line(colour = "gray90"),
  20. panel.grid.minor = element_line(colour = "grey95"),
  21. plot.subtitle = element_text(hjust = 1),
  22. axis.text.x = element_text(angle = 90),
  23. strip.background = element_blank(),
  24. strip.text = element_blank()
  25. ) +
  26. scale_x_discrete(limits = 1:9)
  27. p + facet_wrap(~facets_2 * ~facets)

Please note that I've translated the code and removed the non-code content as requested.

英文:

How about this using facet_wrap?

  1. p&lt;-ggplot(tmp) +
  2. geom_tile(
  3. aes(
  4. x = p1_age_cat_complete,
  5. y = reorder(channel_topic, -group_id),
  6. fill = n
  7. )
  8. ) + theme(
  9. axis.text.x = element_text(angle = 90)
  10. ) +
  11. labs(
  12. x = &quot;Alter&quot;,
  13. y = &quot;Channel Topic&quot;,
  14. fill = &quot;Anzahl&quot;,
  15. subtitle = paste(&quot;N = &quot;, sum(tmp$n), sep = &quot;&quot;)
  16. ) + theme(
  17. panel.background = element_rect(fill = &quot;white&quot;,
  18. colour = &quot;white&quot;),
  19. panel.grid.major = element_line(colour = &quot;gray90&quot;),
  20. panel.grid.minor = element_line(colour = &quot;grey95&quot;),
  21. plot.subtitle = element_text(hjust = 1),
  22. axis.text.x = element_text(angle = 90),
  23. strip.background = element_blank(),
  24. strip.text = element_blank()
  25. ) +
  26. scale_x_discrete(limits = 1:9)
  27. p+facet_wrap(~facets_2 * ~facets)

huangapple
  • 本文由 发表于 2023年8月4日 23:32:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定