使用 ggplot 绘制带有标签的不同颜色的条形图
draw barplot with different color with labels, using ggplot
我想绘制不同颜色的条形图。数据是时间序列数据,看起来像:
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
我想使用 ggplot 将此时间序列数据绘制为条形图。困难的部分是,
1) 我想画黑色,标签 = 1,红色标签 = 2,蓝色标签 = 3
2) 我想在每个每日柱的顶部绘制标签“1”、“2”、“3”。
对于着色,您可以使用 scale_fill_manual
,对于标签,您可以使用 text
(使用 vjust
参数来调整标签在栏上的显示位置)。
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'))
我想绘制不同颜色的条形图。数据是时间序列数据,看起来像:
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
我想使用 ggplot 将此时间序列数据绘制为条形图。困难的部分是,
1) 我想画黑色,标签 = 1,红色标签 = 2,蓝色标签 = 3
2) 我想在每个每日柱的顶部绘制标签“1”、“2”、“3”。
对于着色,您可以使用 scale_fill_manual
,对于标签,您可以使用 text
(使用 vjust
参数来调整标签在栏上的显示位置)。
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'))