R中gglikert图表总计显示异常的调整方法问询
解决方案
1. 调整总计的位置与对齐参数
gglikert()的totals_x和totals_hjust参数可以直接控制总计文本的位置:
- 给左侧总计(反对类总和)设置
totals_x = -0.1、totals_hjust = 1(右对齐,贴近左边框) - 给右侧总计(赞同类总和)设置
totals_x = 1.1、totals_hjust = 0(左对齐,贴近右边框)
修改后的gglikert调用代码:
gg <- gglikert( df, labels_size = 2, y_label_wrap = 68, totals_hjust = c(1, 0), # 左总计右对齐,右总计左对齐 totals_x = c(-0.1, 1.1) # 指定总计x坐标,贴近边框 )
2. 扩展x轴范围避免总计被裁剪
默认x轴范围是[-1,1],右侧总计的x=1.1会超出范围,需要用scale_x_continuous()扩展x轴极限:
gg <- gg + scale_x_continuous(limits = c(-1.2, 1.2), breaks = seq(-1, 1, 0.5))
3. 加大右侧边距防止HTML渲染裁剪
原代码右侧边距过小,加大后能避免HTML容器边缘截断右侧总计:
gg + theme( legend.position = "inside", legend.position.inside = c(0, -0.1), legend.direction = "horizontal", panel.grid = element_blank(), plot.margin = margin( t = 10, r = 30, # 加大右边距 b = 25, l = 10 ) )
完整调整后代码
library(dplyr, warn = FALSE) library(ggplot2) library(ggstats) library(purrr) library(scales) likert_levels <- c( "Strongly disagree", "Disagree", "Neither agree nor disagree", "Agree", "Strongly agree" ) df <- tibble( "there is an argument sort_prop_include_center that could be set up to TRUE in order to include half of the centered level when sorting data" = sample(likert_levels, 150, replace = TRUE), "Similarly, the argument totals_include_center allows you to include half of the centered level into the left and the right totals" = sample(likert_levels, 150, replace = TRUE, prob = 5:1), "Here is one possible option which uses reorder and an ifelse to reorder the variable mapped on y using the counts (aka the sum) of Strictly disagree " = sample(likert_levels, 150, replace = TRUE, prob = 1:5), "and disagree answers. Under the hood ggstats::gglikert reshape the data to long where the question id's are stored in a column named .question and the answers in a column named .answer:" = sample(likert_levels, 150, replace = TRUE, prob = 1:5), "They used sampling data and create a data frame called df. I am using the same as given in the link. (the df not the df_dk). Ok, if i run in R the following code :" = sample(c(likert_levels, NA), 150, replace = TRUE), "proportion of answers higher than the centered level. I want the plot to be sorted according to very left proportions that are the combination (sum) of the two lower levels. (i.e. Respectively the percentages on the very right in the sum of the two lower categories. )" = sample( likert_levels, 150, replace = TRUE, prob = c(1, 0, 1, 1, 0) ) ) %>% mutate(across(everything(), ~ factor(.x, levels = likert_levels))) anno <- partial( annotate, "text", x = -1, angle = 90, size = 3, fontface = "bold" ) gg <- gglikert( df, labels_size = 2, y_label_wrap = 68, totals_hjust = c(1, 0), # 左总计右对齐,右总计左对齐 totals_x = c(-0.1, 1.1) # 指定总计x位置,贴近边框 ) + aes(y = reorder( .question, ifelse(.answer %in% c("Strongly disagree", "Disagree"), 1, 0), FUN = sum, decreasing = TRUE )) + theme(axis.text.y = element_text(size = 8)) + geom_hline( yintercept = c(1.5, 5.5), linetype = "dashed", colour = "grey20" ) + anno(y = 1, label = "warning\nzone", color = "#A6611A") + anno(y = 3.5, label = "so-so", color = "black") + anno(y = 6, label = "no warning\nzone", color = "#018571") + labs(y = NULL) + scale_x_continuous(limits = c(-1.2, 1.2), breaks = seq(-1, 1, 0.5)) # 扩展x轴范围 gg + theme( legend.position = "inside", legend.position.inside = c(0, -0.1), legend.direction = "horizontal", panel.grid = element_blank(), plot.margin = margin( t = 10, r = 30, # 加大右边距 b = 25, l = 10 ) )
关键调整说明
totals_x和totals_hjust:精准控制总计文本的位置与对齐,让左右总计分别贴近对应边框scale_x_continuous(limits):扩展x轴范围,确保总计文本不会超出绘图区域被截断- 加大右侧边距:避免HTML渲染时,容器边缘裁剪右侧总计内容
内容的提问来源于stack exchange,提问作者Homer Jay Simpson
相关产品推荐
相关产品推荐

