如何修复数据标签的 likert() 裁剪
How do I fix likert() clipping of data labels
我正在使用 likert() 生成调查数据图。图为百分比水平的裁剪显示,如下:
这对于同一数据集中的其他类似图来说更严重/更不严重(所以这里只是百分比符号,但在其他情况下会剪成数字)- 我想知道是否有人有好主意如何调整这一点李克特显示,使标签在绘图网格内。
# data for reproducible example:
q25_data <- structure(list(Q25_self_and_family = c(4, 2, 3, 5, 3, 3, 4, 2,
4, 2, 4, 4, 3, 3, 2, 5, 3, 4, 1, 3, 3, 2, 4, 2, 2, 2, 4, 3, 3,
3, 2, 5, 5, 4, 2, 2, 2, 3, 1, 3, 2, 1, 2, 4, 2), Q25_local_area = c(3,
3, 3, 5, 3, 2, 4, 2, 4, 2, 4, 3, 2, 3, 2, 5, 4, 5, 1, 4, 3, 3,
4, 2, 3, 2, 3, 3, 2, 3, 2, 5, 5, 2, 2, 2, 2, 3, 1, 1, 2, 1, 2,
4, 3), Q25_uk = c(4, 3, 3, 5, 2, 3, 5, 2, 4, 2, 4, 3, 3, 3, 3,
5, 4, 5, 2, 3, 3, 2, 4, 2, 4, 3, 4, 3, 2, 4, 4, 5, 5, 4, 3, 3,
2, 4, 2, 5, 2, 2, 2, 3, 3), Q25_outside_uk = c(4, 4, 3, 5, 4,
4, 5, 2, 4, 3, 3, 3, 3, 4, 3, 5, 4, 5, 4, 3, 3, 2, 4, 2, 5, 3,
3, 2, 2, 3, 4, 4, 5, 4, 4, 3, 2, 4, 4, 5, 2, 3, 2, 2, 2)), row.names = c(NA,
-45L), class = c("tbl_df", "tbl", "data.frame"))
# libraries:
library(tidyverse)
library(likert)
# here goes!
title <- "How serious a threat do you think \nclimate change poses to the following?"
names(q25_data) <- c("You and your family in the UK", "People in your local area or city", "The UK as a whole", "Your family and/or friends living outside the UK")
# Set up levels text for question responses
q25_levels <- paste(c("not at all", "somewhat", "moderately", "very", "extremely"),
"serious")
q25_likert_table <- q25_data %>%
mutate(across(everything(),
factor, ordered = TRUE, levels = 1:5, labels=q25_levels)) %>%
as.data.frame %>%
likert
plot(q25_likert_table, wrap=20, text.size=3, ordered=TRUE, low.color='#B18839', high.color='#590048') +
ggtitle(title) +
labs(title = title, y="") +
guides(fill = guide_legend(title = NULL)) +
theme_ipsum_rc() +
theme(axis.text.y = element_text(size = 9))
使用 out-of-the-box 选项时,这总是麻烦。 (:
每个 ggplot 的两个快速选项是扩展 limits
或增加比例的扩展(例如 expand = c(.1, 0)
)以为标签腾出更多空间。
请注意,likert
在后台使用 coord_flip
,因此相关比例为 scale_y_continuous
。默认情况下,限制设置为 -105 到 105,我增加到 -110 到 110。此外,对于标签,使用 abs
olute 值。
p <- plot(q25_likert_table, wrap = 20, text.size = 3, ordered = TRUE, low.color = "#B18839", high.color = "#590048") +
ggtitle(title) +
labs(title = title, y = "") +
guides(fill = guide_legend(title = NULL)) +
theme_ipsum_rc() +
theme(axis.text.y = element_text(size = 9))
p +
scale_y_continuous(labels = abs, limits = c(-110, 110))
#> Scale for 'y' is already present. Adding another scale for 'y', which will
#> replace the existing scale.
我正在使用 likert() 生成调查数据图。图为百分比水平的裁剪显示,如下:
这对于同一数据集中的其他类似图来说更严重/更不严重(所以这里只是百分比符号,但在其他情况下会剪成数字)- 我想知道是否有人有好主意如何调整这一点李克特显示,使标签在绘图网格内。
# data for reproducible example:
q25_data <- structure(list(Q25_self_and_family = c(4, 2, 3, 5, 3, 3, 4, 2,
4, 2, 4, 4, 3, 3, 2, 5, 3, 4, 1, 3, 3, 2, 4, 2, 2, 2, 4, 3, 3,
3, 2, 5, 5, 4, 2, 2, 2, 3, 1, 3, 2, 1, 2, 4, 2), Q25_local_area = c(3,
3, 3, 5, 3, 2, 4, 2, 4, 2, 4, 3, 2, 3, 2, 5, 4, 5, 1, 4, 3, 3,
4, 2, 3, 2, 3, 3, 2, 3, 2, 5, 5, 2, 2, 2, 2, 3, 1, 1, 2, 1, 2,
4, 3), Q25_uk = c(4, 3, 3, 5, 2, 3, 5, 2, 4, 2, 4, 3, 3, 3, 3,
5, 4, 5, 2, 3, 3, 2, 4, 2, 4, 3, 4, 3, 2, 4, 4, 5, 5, 4, 3, 3,
2, 4, 2, 5, 2, 2, 2, 3, 3), Q25_outside_uk = c(4, 4, 3, 5, 4,
4, 5, 2, 4, 3, 3, 3, 3, 4, 3, 5, 4, 5, 4, 3, 3, 2, 4, 2, 5, 3,
3, 2, 2, 3, 4, 4, 5, 4, 4, 3, 2, 4, 4, 5, 2, 3, 2, 2, 2)), row.names = c(NA,
-45L), class = c("tbl_df", "tbl", "data.frame"))
# libraries:
library(tidyverse)
library(likert)
# here goes!
title <- "How serious a threat do you think \nclimate change poses to the following?"
names(q25_data) <- c("You and your family in the UK", "People in your local area or city", "The UK as a whole", "Your family and/or friends living outside the UK")
# Set up levels text for question responses
q25_levels <- paste(c("not at all", "somewhat", "moderately", "very", "extremely"),
"serious")
q25_likert_table <- q25_data %>%
mutate(across(everything(),
factor, ordered = TRUE, levels = 1:5, labels=q25_levels)) %>%
as.data.frame %>%
likert
plot(q25_likert_table, wrap=20, text.size=3, ordered=TRUE, low.color='#B18839', high.color='#590048') +
ggtitle(title) +
labs(title = title, y="") +
guides(fill = guide_legend(title = NULL)) +
theme_ipsum_rc() +
theme(axis.text.y = element_text(size = 9))
使用 out-of-the-box 选项时,这总是麻烦。 (:
每个 ggplot 的两个快速选项是扩展 limits
或增加比例的扩展(例如 expand = c(.1, 0)
)以为标签腾出更多空间。
请注意,likert
在后台使用 coord_flip
,因此相关比例为 scale_y_continuous
。默认情况下,限制设置为 -105 到 105,我增加到 -110 到 110。此外,对于标签,使用 abs
olute 值。
p <- plot(q25_likert_table, wrap = 20, text.size = 3, ordered = TRUE, low.color = "#B18839", high.color = "#590048") +
ggtitle(title) +
labs(title = title, y = "") +
guides(fill = guide_legend(title = NULL)) +
theme_ipsum_rc() +
theme(axis.text.y = element_text(size = 9))
p +
scale_y_continuous(labels = abs, limits = c(-110, 110))
#> Scale for 'y' is already present. Adding another scale for 'y', which will
#> replace the existing scale.