在R中创建分组条形图

时间:2019-05-30 18:15:37

标签: r

我在R中有一个数据框,看起来像这样:

 Requirements   Documentation   Development  Delivery

 After          After           During       During
 Before         After           Before       After
 After          Before          Before       During
 During         During          After        Before
...

我需要创建一个直方图,其外观必须类似于下图,但是我无法创建正确的数据集

enter image description here

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

请检查以下内容:https://stackoverflow.com/help/minimal-reproducible-example

  df <- data.frame(
      Requirement = c("After", "Before", "After", "During"),
      Documentation = c("After", "After", "Before", "During"),
      Development = c("During", "Before", "Before", "After"),
      Delivery = c("During", "After", "During", "Before")
    )

您首先必须以长格式重塑数据框

library(tidyr)
newdf <- gather(df)


p <- ggplot(data=newdf, aes(x=key, y=value, fill = value)) +
  geom_bar(stat="identity", color="black", position=position_dodge())+
  theme_minimal()
p