On 10/15/2012 11:46 AM, rb wrote:
> Hi - Please see sample code. I am trying to plot a barchart of a simple
> timeseries. Barcharts produce unnecessary spacing for weekends, in order to
> counter that I make my x axis as a character. however on doing that the
> labels on the x axis are not correct (showing jan dates instead of
> october). Any advice?
Its' worth pointing out timeSequence is in the timeDate pacakge
> tS = timeSequence(from = paste('2010', "-10-01", sep = ""),to =
> paste('2010', "-10-30", sep = ""))
> dates <-as.Date(as.character(tS[which(!isWeekend(tS))]))
> values <- c(1:NROW(dates))
> mydata <- data.frame(dates, values)
>
> # -- plot in date format
> ggplot(mydata, aes(x=dates, y=values)) + geom_bar(stat = "identity") +
> scale_x_date(major = '1 week')
>
> # -- dates in character, fixes the spacing issue but now the x axis labels
> are messed up
> mydata2 <-data.frame(as.character(dates), values)
> colnames(mydata2) <- c('dates', 'values')
> ggplot(mydata2, aes(x=dates, y=values)) + geom_bar(stat = "identity")
> ggplot(mydata2, aes(x=dates, y=values)) + geom_bar(stat = "identity") +
> scale_x_date(major = '1 week')
I don't see dates from January; I still see dates in October. The last
of these two won't work because the x-axis is now character (discrete),
not date.
Using
ggplot(mydata2, aes(x=dates, y=values)) + geom_bar(stat = "identity")
I get labels "2010-10-01", "2010-10-04", "2010-10-05", etc., in various
amounts of overlap (depending on how big I make the window). I can
rotate the labels to make them easier to read
ggplot(mydata2, aes(x=dates, y=values)) + geom_bar(stat = "identity") +
theme(axis.text.x=element_text(angle=90, vjust=0.5))
Or, since it seems you are using an older version of ggplot2,
ggplot(mydata2, aes(x=dates, y=values)) + geom_bar(stat = "identity") +
opts(axis.text.x=theme_text(angle=90))
Why do you think the dates are in January?
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University