英文:
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)
输出如下所示:
如何去除绘图左侧的 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 <- 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"),
p1_age_cat_complete = factor(p1_age_cat_complete))
tmp %>%
ggplot() +
facet_grid(
cols = vars(facets_2),
rows = vars(facets),
scales = "free",
space = "free"
) +
geom_tile(
aes(
x = p1_age_cat_complete,
y = reorder(channel_topic, -group_id),
fill = n
)
) +
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()
)
<!-- -->
<sup>创建于2023年08月04日,使用 reprex v2.0.2</sup>
英文:
Is this what you're looking for?
p1_age_cat_complete
is made into a factor- Set
scales
andspace
free for the x axis in addition to the y - 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 <- 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"),
p1_age_cat_complete = factor(p1_age_cat_complete))
tmp %>%
ggplot() +
facet_grid(
cols = vars(facets_2),
rows = vars(facets),
scales = "free",
space = "free"
) +
geom_tile(
aes(
x = p1_age_cat_complete,
y = reorder(channel_topic, -group_id),
fill = n
)
) +
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()
)
<!-- -->
<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<-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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论