library(tidyverse)
Create some data:
smarket_summary <- data.frame(
year = 2001:2004,
up = sample(100:200, 4),
down = sample(100:200, 4)
)
smarket_summary
## year up down
## 1 2001 154 109
## 2 2002 121 122
## 3 2003 184 197
## 4 2004 134 188
Reshape the data:
smarket_summary <- smarket_summary %>%
gather(direction, value, up, down)
smarket_summary
## year direction value
## 1 2001 up 154
## 2 2002 up 121
## 3 2003 up 184
## 4 2004 up 134
## 5 2001 down 109
## 6 2002 down 122
## 7 2003 down 197
## 8 2004 down 188
Plot the data:
ggplot(smarket_summary, aes(year, value, fill=direction)) +
geom_bar(stat="identity", position='dodge')
```