【问题标题】:Change the order of stacked histogram bars in ggplot更改ggplot中堆叠直方图条的顺序
【发布时间】:2020-01-30 06:59:32
【问题描述】:

我在 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 = ""))  

如何让蓝色(“超级”)条位于红色(“常规”)条之上?

【问题讨论】:

标签: r ggplot2 histogram


【解决方案1】:

您可以在调用ggplot之前更改df中因子的顺序

df$Item_Pool = factor(df$Item_Pool,levels=c("Super","Regular"))

按你需要的顺序排列关卡

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 2017-07-21
    • 2018-02-15
    • 2022-08-19
    • 2017-09-17
    • 1970-01-01
    相关资源
    最近更新 更多