1
votes

I want to draw bar plot with different colors. The data is time series data, looks like:

date          volume         label
2015-02-16    102              1 
2015-02-17    112              1 
2015-02-18    152              2 
2015-02-19    132              1 
2015-02-20    122              1 
2015-02-21    92               3

I want to draw this time series data as bar plot using ggplot. The difficulty parts are that,

1) I want to draw black color, with label = 1, red color with label = 2 and blue color with label = 3

2) I want to plot the label "1", "2", "3" on the top of each daily bar.

1

1 Answers

4
votes

For coloring you can use scale_fill_manual, for labels you can use text (use the vjust argument to adjust where the labels appear on the bar).

library(ggplot2)
ggplot(dat, aes(date, volume, fill=factor(label))) +
  geom_bar(stat='identity') +
  geom_text(aes(label=label), color="white", vjust=2) +
  scale_fill_manual(breaks=levels(dat$label), values=c('black', 'red', 'blue'))

enter image description here