需要创建堆栈条形图

时间:2012-03-14 22:10:10

标签: r

我有三列日期,var1和var2。我想创建条形图日期为x轴,var1和var2为y轴。我可以在折线图中做到这一点,但我真的很想看看如何在带有时间序列数据的条形图中完成这项工作。

我的数据是这样的:

Date         var1     var2
2011-12-06  37608.1    12304.2
2011-12-07  76430.9    28617.7
2011-12-08  93112.3    33414.6
2011-12-09 100334.8    28112.0
2011-12-10  70474.0    23641.4
2011-12-11 231113.6    78172.5

我执行了这个:

x<-melt(diskIO, id=c("Date"))

然后

qplot(x = factor(Date), y = value, data = x, geom = "bar", fill = variable)

但是轴上的日期并不是真的可读,有人建议稍微更好地组织日期。

1 个答案:

答案 0 :(得分:3)

这与ggplot2

有什么关系
# load libraries
library(ggplot2)
library(reshape2)
# load data
df1 <- read.table(header=TRUE, text=
"Date         var1     var2
2011-12-06  37608.1    12304.2
2011-12-07  76430.9    28617.7
2011-12-08  93112.3    33414.6
2011-12-09 100334.8    28112.0
2011-12-10  70474.0    23641.4
2011-12-11 231113.6    78172.5")
# reshape for plotting
df1.m <-melt(df1)
# make a quick plot
qplot(x = factor(Date), y = value, data = df1.m, geom = "bar", fill = variable)

由此产生的情节......

enter image description here