【问题标题】:How to keep sequential order of events when adding color to barplot in ggplot在ggplot中向条形图添加颜色时如何保持事件的顺序
【发布时间】:2017-08-08 21:13:47
【问题描述】:

我正在尝试创建一个情节来描述我的学生随着时间的推移的行为。添加颜色时,我无法获得正确的事件顺序。例如,当我使用以下代码时,我得到了正确的事件顺序,但没有得到颜色:

ggplot(data=activity_timeline, aes(x=" ", y = Total.Time))+
  geom_bar(stat="identity", color="black")+
  facet_grid(facets=Name~.)+
  scale_fill_manual(values = c("red", "blue", "gray", "yellow", "purple", "orange", "green"),
                    name= "The student was doing:",
                    labels = c("Analysis", "Implementation", "None", "Organizing", "Planning", "Research", "Verification"))+
  ggtitle("                                           Time Spent on Problem Solving Activities")+
  labs(y = "Time in seconds", x = " ")+
  coord_flip()+
  theme(legend.position = "bottom", plot.title = element_text(size = 12),
        strip.text.y = element_text(size=12),
        strip.text.x = element_text(size=12),
        axis.title.x=element_text(size=12),
        legend.text=element_text(size=12),
        legend.title=element_text(size=12))

正确的顺序,没有颜色

当我添加颜色选项(第 2 行)时,图表会将具有相同操作的所有项目组合在一起。我使用的代码是:

ggplot(data=activity_timeline, aes(x=" ", y = Total.Time))+
  geom_bar(stat="identity", color="black", aes(fill=Action))+
  facet_grid(facets=Name~.)+
  scale_fill_manual(values = c("red", "blue", "gray", "yellow", "purple", "orange", "green"),
                    name= "The student was doing:",
                    labels = c("Analysis", "Implementation", "None", "Organizing", "Planning", "Research", "Verification"))+
  ggtitle("                                           Time Spent on Problem Solving Activities")+
  labs(y = "Time in seconds", x = " ")+
  coord_flip()+
  theme(legend.position = "bottom", plot.title = element_text(size = 12),
        strip.text.y = element_text(size=12),
        strip.text.x = element_text(size=12),
        axis.title.x=element_text(size=12),
        legend.text=element_text(size=12),
        legend.title=element_text(size=12))

错误的顺序,有颜色

我对 ggplot 完全陌生,并且使用我在网上找到的代码来执行此操作。我试图使用“时间线”功能,但我不知道如何让它与我的数据一起使用。如果有人对如何修复此代码有任何建议,我将不胜感激。

【问题讨论】:

    标签: r ggplot2 colors geom-bar


    【解决方案1】:

    如果您希望按特定顺序排列堆叠条形,您可以在数据集中创建一个额外的列来指示该顺序。否则,我相信 geom_bar 默认按填充颜色对条形图进行排序。

    这是一个示例数据集用于说明:

    set.seed(1)
    df <- data.frame(t = rexp(100, rate = 0.1),
                     student = c(rep("s1", 50), rep("s2", 50)),
                     activity = sample(c("a", "b", "c"), 100, replace = T),
                     seq = c(seq(1, 50, 1), seq(1, 50, 1)))
    
    # without group parameter
    ggplot(df, aes(x = "", y = t, fill = activity)) +
      geom_bar(stat = "identity", colour = "black") +
      facet_grid(student~.) +
      labs(y="time", x = "") +
      coord_flip()
    

    # with group parameter
    ggplot(df, aes(x = "", y = t, fill = activity, group = seq)) +
      geom_bar(stat = "identity", colour = "black") +
      facet_grid(student~.) +
      labs(y="time", x = "") +
      coord_flip()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-21
      • 2021-12-02
      • 1970-01-01
      • 2021-07-16
      • 2020-10-05
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多