admin管理员组文章数量:1297079
I have the same simulated data as described here with the difference of the group 2 has elements of big text. I want to flip horizontally the text in facet rows to but without disappearing the facet col strip at the top.
How can I do it in R?
library(ggstats)
library(dplyr)
library(ggplot2)
likert_levels <- c(
"Strongly disagree",
"Disagree",
"Neither agree nor disagree",
"Agree",
"Strongly agree"
)
df <-
tibble(
grouping = sample(c("A", "B", "C", "D"), 150, replace = TRUE),
q1 = sample(likert_levels, 150, replace = TRUE),
q2 = sample(likert_levels, 150, replace = TRUE, prob = 5:1),
q3 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
q4 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
q5 = sample(c(likert_levels, NA), 150, replace = TRUE),
q6 = sample(likert_levels, 150, replace = TRUE, prob = c(1, 0, 1, 1, 0))
) %>%
mutate(across(-grouping, ~ factor(.x, levels = likert_levels)))
df
gglikert(df, q1:q6, facet_cols = vars(grouping))
df_group <- df
df_group$group1 <- sample(c("category A", "category B","category C"), 150, replace = TRUE)
df_group$group2 <- sample(c("usa-new york", "usa-san fransisco", "usa-new orleans",
"united kingdom-stratford upon avon",
"south africa-port elizabeth",
"new zealand-upper hutt city"), 150, replace = TRUE)
gglikert(df_group,
q3:q6,
facet_cols = vars(group1),
facet_rows = vars(group2),
labels_size = 3
) +
scale_x_continuous(
labels = label_percent_abs(),
expand = expansion(0, .2)
)
I have the same simulated data as described here with the difference of the group 2 has elements of big text. I want to flip horizontally the text in facet rows to but without disappearing the facet col strip at the top.
How can I do it in R?
library(ggstats)
library(dplyr)
library(ggplot2)
likert_levels <- c(
"Strongly disagree",
"Disagree",
"Neither agree nor disagree",
"Agree",
"Strongly agree"
)
df <-
tibble(
grouping = sample(c("A", "B", "C", "D"), 150, replace = TRUE),
q1 = sample(likert_levels, 150, replace = TRUE),
q2 = sample(likert_levels, 150, replace = TRUE, prob = 5:1),
q3 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
q4 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
q5 = sample(c(likert_levels, NA), 150, replace = TRUE),
q6 = sample(likert_levels, 150, replace = TRUE, prob = c(1, 0, 1, 1, 0))
) %>%
mutate(across(-grouping, ~ factor(.x, levels = likert_levels)))
df
gglikert(df, q1:q6, facet_cols = vars(grouping))
df_group <- df
df_group$group1 <- sample(c("category A", "category B","category C"), 150, replace = TRUE)
df_group$group2 <- sample(c("usa-new york", "usa-san fransisco", "usa-new orleans",
"united kingdom-stratford upon avon",
"south africa-port elizabeth",
"new zealand-upper hutt city"), 150, replace = TRUE)
gglikert(df_group,
q3:q6,
facet_cols = vars(group1),
facet_rows = vars(group2),
labels_size = 3
) +
scale_x_continuous(
labels = label_percent_abs(),
expand = expansion(0, .2)
)
Share
asked Feb 11 at 16:35
Homer Jay SimpsonHomer Jay Simpson
1,2328 silver badges35 bronze badges
1 Answer
Reset to default 1You can achieve your desired result using
+ theme(strip.text.y = element_text(angle = 0))
And as asked for in the comments, you can wrap the text by overwriting the default facet_grid
layer and using label_wrap_gen()
to wrap the strip text for the rows.
gglikert(df_group,
q3:q6,
facet_cols = vars(group1),
facet_rows = vars(group2),
labels_size = 3
) +
scale_x_continuous(
labels = label_percent_abs(),
expand = expansion(0, .2)
) +
facet_grid(
cols = vars(group1),
rows = vars(group2),
labeller = labeller(
.rows = label_wrap_gen(width = 20)
)
) +
theme(strip.text.y = element_text(angle = 0))
本文标签: ggplot2Flip the text in facet rows in gglikert in RStack Overflow
版权声明:本文标题:ggplot2 - Flip the text in facet rows in gglikert in R - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741647240a2390258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论