-3

I have three columns with date, var1 and var2. I like to create bar chart date being the x axis and var1 and var2 is y axis. I can do this in line chart but I really like to see how is this done in bar chart with time series data.

My data would be like this:

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

I performed this:

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

then

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

but the date on axis is not really readable, can somebody suggest to organize the date little better.

george willy
  • 1,613
  • 6
  • 20
  • 26

1 Answers1

3

How about this, with 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)

And the resulting plot...

enter image description here

Ben
  • 38,669
  • 17
  • 120
  • 206
  • Thanks Ben. It is not working for me. I have a data frame that was written in from a file. Date, var1 and var2 columns. I melt it and issue your qplot command but getting and error variable object not found. – george willy Mar 15 '12 at 14:07
  • when I do melt on my data frame, I get this error: Using as id variables Error in as.POSIXct.numeric(value) : 'origin' must be supplied – george willy Mar 15 '12 at 14:12
  • 1
    @mikesmith That means your real data frame isn't the same as the data you provided in your question. If you provide us with bad information, you will likely get misleading answers. Try editing your question so that it actually reflects the structure of your data. The functions `str` and `dput` may be useful here. – joran Mar 15 '12 at 14:41
  • no bad data, it is the same. I have 3 columns date, var1 and var2. do you suggest I should convert date to str first before melting? – george willy Mar 15 '12 at 14:51
  • @joran I was able do the graph, I added my original quesion. Is there a way to modify the xaxis so that dates are little organized. Since there are too many data points, dates are not readable? thank you so much for your help. This site is great. – george willy Mar 15 '12 at 14:59
  • Perhaps you want to rotate the tick labels, something like this: http://stackoverflow.com/questions/1330989/rotating-and-spacing-axis-labels-in-ggplot2 – Ben Mar 15 '12 at 15:34
  • For reference: Under current library versions, this returns `Error: stat_count() must not be used with a y aesthetic.` – SAFEX Jun 21 '19 at 13:57