John
John

Reputation: 643

How to make histogram with time on horizontal axis in ggplot2 (R)

I know how to make a histogram with regular numerical values, however I want to be able to do something similar to the following:enter image description here

I am wondering how to make a histogram for the values t-1, t-2 and t-3 at the horizontal axis in ggplot2.

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()

Upvotes: 1

Views: 109

Answers (1)

John
John

Reputation: 643

For those interested, I got the desired result as follows:

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())

It ended up looking like this;

enter image description here

Upvotes: 2

Related Questions