1

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()
hpesoj626
  • 3,529
  • 1
  • 17
  • 25
John
  • 555
  • 3
  • 16
  • 1
    Can you show us your initial work and point out where you are having a problem? Please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – hpesoj626 May 27 '18 at 13:57
  • I added the code that I have so far, but am not satisfied with it yet. In particular, I want to column on the RHS to be a bit further away from the other three. – John May 27 '18 at 14:04
  • 1
    Your `trt` values aren't times, though, they're strings. A histogram is for the distribution of continuous values. You could change `trt` to numeric values where maybe each differs by 1, except `t` differs by 1.5 from the nearest value, or something like that to nudge it away. – camille May 27 '18 at 14:14
  • The order of the treatments can be changed if you define them as `factor`. Then, it will be th order of the levels that is used: `trt = factor(c("t-3", "t-2", "t-1", "t"), levels = c("t-3", "t-2", "t-1", "t"))`. – Rui Barradas May 27 '18 at 14:17

1 Answers1

2

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

John
  • 555
  • 3
  • 16