[英]Reordering factor for plotting using forcats and ggplot2 packages from tidyverse
First of all, thanks ^13 to tidyverse . 首先,感谢tidyverse ^ 13 。 I want the bars in the chart below to follow the same factor levels reordered by
forcats::fct_reorder ()
. 我希望下表中的柱线遵循由
forcats::fct_reorder ()
重新排序的相同因子水平。 Surprisingly, I see different order of levels in the data set when View ()
ed as when they are displayed in the chart (see below). 令人惊讶的是,当
View ()
时,在数据集中看到的级别顺序与在图表中显示时的级别不同(请参见下文)。 The chart should illustrate the number of failed students before and after the bonus marks (I want to sort the bars based on the number of failed students before the bonus). 该图表应说明在奖励分数之前和之后失败的学生人数(我想根据奖励之前的失败学生人数对条形进行排序)。
MWE 微机
ggplot (df) +
geom_bar (aes (forcats::fct_reorder (subject, FailNo, .desc= TRUE), FailNo, fill = forcats::fct_rev (Bonus)), position = 'dodge', stat = 'identity') +
theme (axis.text.x=element_text(angle=45, vjust=1.5, hjust=1.5, size = rel (1.2)))
Data output of dput (df) dput(df)的数据输出
structure(list(subject = structure(c(1L, 2L, 5L, 6L, 3L, 7L,
4L, 9L, 10L, 8L, 12L, 11L, 1L, 2L, 5L, 6L, 3L, 7L, 4L, 9L, 10L,
8L, 12L, 11L), .Label = c("CAB_1", "DEM_1", "SSR_2", "RRG_1",
"TTP_1", "TTP_2", "IMM_1", "RRG_2", "DEM_2", "VRR_2", "PRS_2",
"COM_2", "MEB_2", "PHH_1", "PHH_2"), class = "factor"), Bonus = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("After", "Before"), class = "factor"),
FailNo = c(29, 28, 20, 18, 15, 13, 12, 8, 5, 4, 4, 2, 21,
16, 16, 14, 7, 10, 10, 5, 3, 4, 4, 1)), .Names = c("subject",
"Bonus", "FailNo"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-24L))
The issue 问题
According to the table above, SSR_2
var should come in the fifth rank and IMM_1
in the sixth, however in the chart we see these two variables swapping their positions. 根据上表,
SSR_2
var应该排在第五位, IMM_1
应该IMM_1
在第六位,但是在图表中我们看到这两个变量交换了位置。 How to sort it right after tidyverse in this case? 在这种情况下,如何在tidyverse之后对其进行排序?
Use factor
with unique
levels for your x -axis. x轴具有
unique
级别的使用factor
。
ggplot (df) +
geom_bar (aes(factor(forcats::fct_reorder
(subject, FailNo, .desc= TRUE),
levels=unique(subject)),
FailNo,
fill = forcats::fct_rev (Bonus)),
position = 'dodge', stat = 'identity') +
theme(axis.text.x=element_text(angle=45, vjust=1.5, hjust=1.5, size = rel (1.2)))
Edited: @dotorate comment 编辑:@ dotorate评论
Sort failNo before the bonus 奖金前排序不成功
library(dplyr)
df_before_bonus <- df %>% filter(Bonus == "Before") %>% arrange(desc(FailNo))
Use FailNo before the bonus to create the factor 在奖金前使用FailNo来创建因子
df$subject <- factor(df$subject, levels = df_before_bonus$subject, ordered = TRUE)
Updated plot 更新剧情
ggplot(df) +
geom_bar(aes (x = subject, y = FailNo, fill = as.factor(Bonus)),
position = 'dodge', stat = 'identity') +
theme (axis.text.x=element_text(angle=45, vjust=1.5, hjust=1.5, size = rel (1.2)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.