2017-06-13 256 views
0

我有数据框df,有46行和3列。基于一个因子水平的值对ggplot2图进行重新排序

我想通过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

。 。 。但随着program_ID变量重新排序的youth_activity_rc变量Not Focused因子水平值的基础上:

有许多的演示如何做到这一点的单一变量的基础上的问题(即this question),但根据与某个因素水平相关的价值(本例中为Not Focused),我无法找到这样的结果;这似乎很简单,但至少根据其他答案中推荐的解决方案(即使用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")) 

回答

1

通过youth_activity_rctotal_minutes_p订购你的数据集,然后使用fct_inorder从包forcats之前绘制一个选项。

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_relevelarrange ING设置要立足顺序因子的水平作为第一级。例如,如果你想通过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

+0

任何直接的方式,如果不'Focused'没有发生来做到这一点“youth_activity_rc”的第一个级别,而不是将其设置为第一级? –

+1

不是我所知道的,但是如果您不想永久性地改变您的订购因子的水平,可以在'arrange'内完成。请参阅编辑。 – aosmith

1

类似的方法来艾欧史密斯,但没有使用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() 

enter image description here

注意我创建了一个新的变量,名为program_ID_2但你不必

相关问题