我有一个名为p的ggplot对象:

library(dplyr, warn = FALSE)

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)))



library(ggplot2)
library(ggstats)

p = ggstats::gglikert(df) +
  aes(y = reorder(.question,
                  ifelse(
                    .answer %in% c("Strongly disagree", "Disagree"),
                    1, 0
                  ),
                  FUN = sum
  ))+ theme(axis.text.y = element_text(size = 8))

我想横向添加两行:

  1. 现在将y轴分开的线按20%的递减顺序排序,

  2. 第二线为50%.

此外,我希望使用0到20%的文本在左侧面板(图)内写入"警告区",20%到50%的文本"马马虎虎",50%到100%的文本写"无警告区"

如何在这个排序比例Likert图中使用R中的ggplot 2包实现这一目标?

推荐答案

要确定线和文本的位置,请将原始图保存为p,然后使用layer_data(p, 3)判断第3个ggplot层(具有文本百分比的层)后面的数据(特别是xylabel列).

然后将geom_hlineannotate与确定的位置相加.

library(dplyr, warn = FALSE)
library(ggplot2)
library(ggstats)
library(purrr)

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)))

# function to save retyping common arguments
anno <- partial(annotate, "text", x = -1, angle = 90, size = 5, fontface = "bold")

gglikert(df) +
  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)

![](https://i.imgur.com/HJO05PN.png)

创建于2024年4月21日,共有reprex v2.1.0

R相关问答推荐

将带有范围的字符串转换为R中的数字载体

从具有随机模式的字符串中提取值

从嵌套列表中智能提取线性模型系数

基于现有类创建类的打印方法(即,打印tibles更长时间)

在(g)子中使用asserable字符

矩阵%*%矩阵中的错误:需要数字/复杂矩阵/向量参数

获取列中值更改的行号

修改用R编写的用户定义函数

如何在区分不同条件的同时可视化跨时间的连续变量?

将文件保存到新文件夹时,切换r设置以不必创建目录

打印XTS对象

在不丢失空值的情况下取消列出嵌套列表

将重复项转换为NA

用两种 colored颜色 填充方框图

按列中显示的配对组估算NA值

ggplot R:X,Y,Z使用固定/等距的X,Y坐标绘制六边形热图

如何筛选截止年份之前最后一个测量年度的所有观测值以及截止年份之后所有年份的所有观测值

如何从嵌套数据中自动创建命名对象?在R中

替换在以前工作的代码中有x行&q;错误(geom_sf/gganimate/dow_mark)

R中从因数到数字的转换