如何根据与 R ggplot2 中两个方面的因子水平匹配的值对 x 轴进行排序?

问题描述 投票:0回答:1

我正在制作一个堆叠条形图,显示 20 名参与者(参与者)执行了两项相同的任务(任务),使用三种策略(CA.存在:存在、可能存在、缺席)所花费的时间(持续时间)比例。我想按任务分面。

我使用了以下代码并获得了此图:plot

CA_presence %>% ggplot(aes(fill=CA.presence, y= Duration, x= participant, label =    scales::percent(Duration))) + 
geom_bar(width = .7, position="fill", stat="identity") +
scale_y_continuous(labels = scales::label_percent()) +
facet_grid(~task) +
ggtitle("Presence of CA in the dataset") +
theme(plot.title = element_text(size = 15)) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.text = element_text(size = 10)) +
theme(strip.text.x = element_text(size = 13)) +
labs(x = "Participant",
     y = "Proportion of discourse time",
     fill = "CA presence") +
theme(axis.text = element_text(size = 10)) +
theme(axis.title = element_text(size = 10)) +
scale_fill_brewer(palette = "Greens") +
coord_flip()

这是数据集头部的 dput() 输出:

structure(
  list(
    participant = c("L001", "L001", "L002", "L002", "L016", "L016"),
    task = c("T05", "T12", "T05", "T12", "T05", "T12"),
    language = c("French", "French", "French", "French", "French", "French"),
    Duration = c(8823, 46275, 2459, 38193, 20488, 160970),
    CA.presence = c("Presence", "Presence", "Presence", "Presence", "Presence", "Presence")
  ),
  class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
  row.names = c(NA, -6L), groups = structure(
    list(
      participant = c("L001", "L001", "L002", "L002", "L016", "L016"),
      task = c("T05", "T12", "T05", "T12", "T05", "T12"),
      .rows = structure(list(
        1L, 2L, 3L, 4L, 5L, 6L
      ), ptype = integer(0), class = c(
        "vctrs_list_of",
        "vctrs_vctr", "list"
      ))
    ),
    class = c("tbl_df", "tbl", "data.frame"),
    row.names = c(NA, -6L), .drop = TRUE
  )
)
r ggplot2 facet facet-wrap facet-grid
1个回答
0
投票

仍然没有 100% 达到您想要的结果,但我的代码希望可以帮助您入门。一般来说,您可以使用

reorder
创建一个按数值变量的值排序的
factor
。您的情况有点特殊,因为您想按堆叠条形图的一种类别(以及分面变量的一种类别??)重新排序。为此,我们可以使用一个小技巧,这意味着使用
ifelse
将其他类别的值设置为零以进行重新排序,并使用
FUN=sum
。使用这个技巧,我根据任务
reorder
participant
类别的值来
"Presence"
ed
"TO5"

由于您的示例数据仅包含

CA.presence
类别,我创建了一些假示例数据:

set.seed(123)

CA_presence <- expand.grid(
  participant = c("L001", "L002", "L016", "L004", "L005"),
  task = c("T05", "T12"),
  CA.presence = c("Presence", "Possible presence", "Absence")
) |>
  transform(Duration = rnorm(30, 20000, 5000))

library(ggplot2)

CA_presence |>
  ggplot(aes(
    fill = CA.presence,
    x = Duration,
    y = reorder(
      participant,
      ifelse(
        CA.presence == "Presence" & task == "T05",
        -Duration,
        0
      ),
      FUN = sum
    ),
    label = scales::percent(Duration)
  )) +
  geom_col(
    width = .7, position = "fill"
  ) +
  scale_x_continuous(labels = scales::label_percent()) +
  facet_grid(~task) +
  labs(
    x = "Proportion of discourse time",
    y = "Participant",
    fill = "CA presence",
    title = "Presence of CA in the dataset"
  ) +
  theme(axis.text = element_text(size = 10)) +
  theme(axis.title = element_text(size = 10)) +
  theme(plot.title = element_text(size = 15, hjust = 0.5)) +
  theme(legend.text = element_text(size = 10)) +
  theme(strip.text.x = element_text(size = 13)) +
  scale_fill_brewer(palette = "Greens")

© www.soinside.com 2019 - 2024. All rights reserved.