根据因子水平的值对 ggplot2 图的条形图重新排序

标签 r ggplot2

我有 46 行 3 列的数据框 df

我正在尝试通过 program_ID 变量创建 youth_activity_rc 变量值的图,如以下代码/图。 。 。

library(ggplot2)
ggplot(df, aes(x = program_name, y = total_minutes_p, group = youth_activity_rc, fill = youth_activity_rc)) +
    geom_col(position = position_stack(reverse = T)) +
    coord_flip()

geom_col figure

。 。 。但根据 youth_activity_rc 变量的 Not Focused 因素级别的值重新排序 program_ID 变量:

有很多问题演示了如何在单个变量(即 this question )的基础上执行此操作,但我找不到任何问题是在与因子水平相关的值的基础上执行此操作的(在这种情况下不集中);这看起来很简单,但至少基于其他答案中推荐的解决方案(即使用 stats::reorder()dplyr::arrange()),它不是。

数据在这里:

df <- structure(list(program_ID = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("1", "2", "4", "5", 
"6", "7", "8", "9", "10"), class = "factor"), youth_activity_rc = structure(c(2L, 
6L, 5L, 1L, 3L, 2L, 6L, 1L, 3L, 2L, 6L, 5L, 1L, 3L, 2L, 6L, 4L, 
5L, 1L, 3L, 2L, 6L, 5L, 1L, 3L, 2L, 6L, 1L, 3L, 2L, 6L, 4L, 1L, 
3L, 2L, 6L, 4L, 5L, 1L, 3L, 2L, 6L, 4L, 5L, 1L, 3L), .Label = c("Not Focused", 
"Basic Skills Activity", "Program Staff Led", "Field Trip Speaker", 
"Lab Activity", "Creating Product"), class = "factor"), total_minutes_p = c(0.248, 
0.116, 0.075, 0.458, 0.103, 0.466, 0.015, 0.202, 0.317, 0.248, 
0.263, 0.006, 0.372, 0.111, 0.183, 0.172, 0.088, 0.048, 0.305, 
0.203, 0.157, 0.066, 0.079, 0.592, 0.106, 0.128, 0.423, 0.423, 
0.026, 0.176, 0.233, 0.125, 0.426, 0.04, 0.164, 0.188, 0.046, 
0.007, 0.524, 0.072, 0.163, 0.112, 0.013, 0.021, 0.567, 0.124
)), .Names = c("program_ID", "youth_activity_rc", "total_minutes_p"
), row.names = c(NA, -46L), vars = "program_ID", labels = structure(list(
    program_ID = c(1, 2, 4, 5, 6, 7, 8, 9, 10)), .Names = "program_ID", row.names = c(NA, 
-9L), class = "data.frame", vars = "program_ID", drop = TRUE), indices = list(
    0:4, 5:8, 9:13, 14:19, 20:24, 25:28, 29:33, 34:39, 40:45), drop = TRUE, group_sizes = c(5L, 
4L, 5L, 6L, 5L, 4L, 5L, 6L, 6L), biggest_group_size = 6L, class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

最佳答案

通过 youth_activity_rctotal_minutes_p 对数据集进行排序,然后在绘图之前使用 forcats 包中的 fct_inorder 是其中之一选项。

fct_inorder 按照因子在数据集中出现的顺序设置因子的级别,这就是为什么对数据集进行排序以按所需顺序获取 program_ID 的级别的原因需要。

library(dplyr)
library(forcats)

df2 = df %>% 
    ungroup() %>%
    arrange(youth_activity_rc, total_minutes_p) %>%
    mutate(program_ID = fct_inorder(program_ID) )

情节:

ggplot(df2, aes(x = program_ID, y = total_minutes_p, 
             group = youth_activity_rc, 
             fill = youth_activity_rc)) +
    geom_col(position = position_stack(reverse = TRUE)) +
    coord_flip()

enter image description here

排列时使用fct_relevel将您想要作为订单基础的因素的级别设置为第一级别。例如,如果您想要在“创建产品”中按 total_minutes_p 排序的图表,而不是“不重点”:

df2 = df %>% 
    ungroup() %>%
    arrange(fct_relevel(youth_activity_rc, "Creating Product"), total_minutes_p) %>%
    mutate(program_ID = fct_inorder(program_ID) )

enter image description here

关于根据因子水平的值对 ggplot2 图的条形图重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44530110/

相关文章:

r - 使用ggplot2的气泡图

r - 如何在图例和 plotarea 之外注释 ggplot2 qplot? (类似于 mtext())

r - 我如何告诉 summary 不要换行?

r - 将科学记数法转换为数字,保留小数

r - 线性回归 "NA"仅估计最后一个系数

r - 将数据帧转换为时间序列

r - stat_compare_means函数多组比较

r - 使用 ggplot 绘制 parking 场占用的 parking 位

反转堆叠顺序而不影响ggplot2条形图中的图例顺序

emacs - ESS/AucTeX/Sweave 集成