R 根据因子级别的值对ggplot2 plot的条形图重新排序

R 根据因子级别的值对ggplot2 plot的条形图重新排序,r,ggplot2,R,Ggplot2,我有46行3列的数据框df 我试图通过program\u ID变量创建youth\u activity\u 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)

我有46行3列的数据框
df

我试图通过
program\u ID
变量创建
youth\u activity\u 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()

。但是,根据
未聚焦
青年活动
变量的
因子水平,对
计划ID
变量重新排序:

有许多问题证明了如何在单个变量(即)的基础上实现这一点,但我发现没有一个问题是在与因子水平相关的值的基础上实现这一点的(
在这种情况下不聚焦
);这似乎很简单,但至少基于其他答案中推荐的解决方案(即使用
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"))

df根据
youth\u activity\u rc
total\u minutes\u p
对数据集进行排序,然后在绘图之前使用package ForCAT中的
fct\u inorder
是一个选项

fct\u inoorder
按照因子在数据集中的显示顺序设置因子的级别,这就是为什么需要对数据集进行排序以获得所需顺序的
程序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()

使用
fct_relevel
arrange
ing可将您要基于订单的因子的级别设置为第一级。例如,如果您希望在“创建产品”中按
total\u minutes\u p
而不是“未聚焦”排序图形:


根据
青年活动\u rc
总分钟数\u p
对数据集进行排序,然后在绘图之前使用package for CAT中的
fct\u顺序是一种选择

fct\u inoorder
按照因子在数据集中的显示顺序设置因子的级别,这就是为什么需要对数据集进行排序以获得所需顺序的
程序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()

使用
fct_relevel
arrange
ing可将您要基于订单的因子的级别设置为第一级。例如,如果您希望在“创建产品”中按
total\u minutes\u p
而不是“未聚焦”排序图形:


类似于aosmith的方法,但不使用forcats/dplyr进行数据操作。您可以获得所需子集中的顺序,然后重构数据,使级别按该顺序排列。比如:

levs <- df[which(df$youth_activity_rc == "Not Focused"), ] #Get the "Not focused" group
order <- order(levs[,"total_minutes_p"]) #Order within your selected group

df$program_ID_2 <- factor(df$program_ID, levels = levs[order, "program_ID"])

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

levs类似于aosmith,但不使用forcats/dplyr进行数据操作。您可以获得所需子集中的顺序,然后重构数据,使级别按该顺序排列。比如:

levs <- df[which(df$youth_activity_rc == "Not Focused"), ] #Get the "Not focused" group
order <- order(levs[,"total_minutes_p"]) #Order within your selected group

df$program_ID_2 <- factor(df$program_ID, levels = levs[order, "program_ID"])

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

lev如果
Not Focused
恰好不是
youth\u activity\u rc
的第一级,除了将其设置为第一级之外,还有什么简单的方法可以做到这一点?我不知道,但如果您不想永久更改订购因子的级别,可以在
arrange
内完成。请参阅编辑。如果
未聚焦
恰好不是
青年活动的第一级
,除了将其设置为第一级之外,是否有任何简单的方法可以做到这一点?据我所知,这并不多见,但如果您不想永久更改订购因子的级别,可以在
排列
内完成。请参见编辑。