如何在facet_grid中删除特定列?

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

How to get rid of a certain column in facet_grid?

问题

以下是代码的翻译部分:

我有以下示例代码:

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)

输出如下所示:

如何在facet_grid中删除特定列?

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

提前感谢。

最好,

Aaron


<details>
<summary>英文:</summary>
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)


The output looks like this:
[![enter image description here][1]][1]
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.
Thank you in advance.
Best,
Aaron
[1]: https://i.stack.imgur.com/Iu9Fx.png
[2]: https://i.stack.imgur.com/aJSTs.png
</details>
# 答案1
**得分**: 1
这是您正在寻找的吗?
1. `p1_age_cat_complete` 被转化为一个因子
2. 为x轴设置了`scales`和`space`的自由度,除了y轴
3. 删除了 `scale_x_discrete`
_edit_ 我忘记注明我在 `labs()` 之后删除了第一个 `theme(axis.text.x...` 的调用,因为它在那之后重复了。
``` r
library(tidyverse)
tmp &lt;- data.frame(
&quot;channel_topic&quot; = rep(LETTERS[seq(1:16)],9),
&quot;p1_age_cat_complete&quot; = rep(1:9, each = 16),
&quot;n&quot; = sample(100, size = 144, replace = TRUE)) |&gt;
group_by(channel_topic) |&gt;
mutate(n_group = sum(n))|&gt;
group_by(n_group) |&gt;
mutate(group_id = abs(cur_group_id()-17)) |&gt;
ungroup() |&gt;
mutate(group_id = ifelse(str_detect(channel_topic, &quot;N&quot;),
99,
ifelse(str_detect(channel_topic, &quot;O&quot;),
100,
ifelse(str_detect(channel_topic, &quot;P&quot;),
101,
group_id)))) |&gt;
mutate(facets = ifelse(group_id &lt; 99,&quot;1&quot;,&quot;2&quot;),
facets_2 = ifelse(p1_age_cat_complete == &quot;9&quot;, &quot;2&quot;,&quot;1&quot;),
p1_age_cat_complete = factor(p1_age_cat_complete))
tmp %&gt;% 
ggplot() +
facet_grid(
cols = vars(facets_2),
rows = vars(facets),
scales = &quot;free&quot;,
space = &quot;free&quot;
) +
geom_tile(
aes(
x = p1_age_cat_complete,
y = reorder(channel_topic, -group_id),
fill = n
)
) +
labs(
x = &quot;Alter&quot;,
y = &quot;Channel Topic&quot;,
fill = &quot;Anzahl&quot;,
subtitle = paste(&quot;N = &quot;, sum(tmp$n), sep = &quot;&quot;)
) + theme(
panel.background = element_rect(fill = &quot;white&quot;,
colour = &quot;white&quot;),
panel.grid.major = element_line(colour = &quot;gray90&quot;),
panel.grid.minor = element_line(colour = &quot;grey95&quot;),
plot.subtitle = element_text(hjust = 1),
axis.text.x = element_text(angle = 90),
strip.background = element_blank(),
strip.text = element_blank()
) 

如何在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().

library(tidyverse)

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

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

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

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

答案2

得分: 0

Sure, here is the translated code:

p <- ggplot(tmp) +
  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)

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?

p&lt;-ggplot(tmp) +
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 = &quot;Alter&quot;,
y = &quot;Channel Topic&quot;,
fill = &quot;Anzahl&quot;,
subtitle = paste(&quot;N = &quot;, sum(tmp$n), sep = &quot;&quot;)
) + theme(
panel.background = element_rect(fill = &quot;white&quot;,
colour = &quot;white&quot;),
panel.grid.major = element_line(colour = &quot;gray90&quot;),
panel.grid.minor = element_line(colour = &quot;grey95&quot;),
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)
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:

确定