如何在ggplot2(R)中在水平轴上制作时间直方图

时间:2018-05-27 13:53:22

标签: r ggplot2 histogram figure

我知道如何使用常规数值制作直方图,但我希望能够执行类似以下的操作:enter image description here

我想知道如何在t-1的水平轴上为值t-2t-3ggplot2制作直方图。

df <- data.frame(trt = c("t-3", "t-2", "t-1", "t"), outcome = c(3, 6, 9, 5))
ggplot(df, aes(trt, outcome)) +
  geom_col()

1 个答案:

答案 0 :(得分:0)

对于那些感兴趣的人,我得到了如下所需的结果:

df <- data.frame(trt = factor(c("t-3", "t-2", "t-1", "t"),
levels = c("t-3", "t-2", "t-1", "t")), outcome = c(9, 3, 7, 4))

myColors <- c("red","red","red", "blue")
u  <- (9+3+7)/3

ggplot(df, aes(trt, outcome)) + geom_col(colour = "black",
 fill = myColors)+geom_hline(yintercept=u, linetype="dashed",
 color = "black") +  xlab("Week") + ylab("Search Volume") +
 theme_minimal()+theme(axis.text.y=element_blank(),
 axis.ticks.y=element_blank())

最终看起来像这样;

enter image description here