更改ggplot中堆叠直方图条的顺序
Change the order of stacked histogram bars in ggplot
我在 ggplot 中创建了堆叠直方图。我想更改堆叠条的顺序。
我的数据集如下所示:
head(df)
difficulty RegularPool SuperPool Item_Pool
1 -3.1185 1 NA Regular
2 -2.9385 1 NA Regular
3 -2.8100 1 NA Regular
4 -2.2797 1 NA Regular
5 -3.3321 1 NA Regular
6 -3.0996 1 NA Regular
我创建了 ggplot:
ggplot(data = df,
aes(x = difficulty, color = Item_Pool, fill = Item_Pool)) +
geom_histogram(alpha= 0.5, position = "stack", binwidth = 0.1) +
geom_vline(xintercept = logit.placement, linetype = "dashed", color = "blue") + #add placement lines
annotate("text", x = logit.placement, y = 0, angle = 45, label=c("One Below", "Early","Mid", "Late", "One Above"), colour = "blue") + #label placement lines
xlim(-8, 7) +
ylab("Number of Testlets") +
xlab("Testlet Difficulty") +
ggtitle(paste("Grade ", grade, " Overall Cut = ", item.pool.cut, sep = ""))
如何让蓝色 ("Super") 条位于红色 ("Regular") 条之上?
您可以在调用 ggplot
之前更改 df
中的因子顺序
df$Item_Pool = factor(df$Item_Pool,levels=c("Super","Regular"))
按照您需要的顺序排列级别
我在 ggplot 中创建了堆叠直方图。我想更改堆叠条的顺序。
我的数据集如下所示:
head(df)
difficulty RegularPool SuperPool Item_Pool
1 -3.1185 1 NA Regular
2 -2.9385 1 NA Regular
3 -2.8100 1 NA Regular
4 -2.2797 1 NA Regular
5 -3.3321 1 NA Regular
6 -3.0996 1 NA Regular
我创建了 ggplot:
ggplot(data = df,
aes(x = difficulty, color = Item_Pool, fill = Item_Pool)) +
geom_histogram(alpha= 0.5, position = "stack", binwidth = 0.1) +
geom_vline(xintercept = logit.placement, linetype = "dashed", color = "blue") + #add placement lines
annotate("text", x = logit.placement, y = 0, angle = 45, label=c("One Below", "Early","Mid", "Late", "One Above"), colour = "blue") + #label placement lines
xlim(-8, 7) +
ylab("Number of Testlets") +
xlab("Testlet Difficulty") +
ggtitle(paste("Grade ", grade, " Overall Cut = ", item.pool.cut, sep = ""))
如何让蓝色 ("Super") 条位于红色 ("Regular") 条之上?
您可以在调用 ggplot
df
中的因子顺序
df$Item_Pool = factor(df$Item_Pool,levels=c("Super","Regular"))
按照您需要的顺序排列级别