bar chart spacing due to weekends

116 views
Skip to first unread message

rb

unread,
Oct 15, 2012, 2:46:27 PM10/15/12
to ggp...@googlegroups.com
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?
 
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')

Brian Diggs

unread,
Oct 15, 2012, 7:05:55 PM10/15/12
to rb, ggplot2
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

rb

unread,
Oct 16, 2012, 11:52:29 AM10/16/12
to ggp...@googlegroups.com, rb
Thanks Brian. A lot of my issues got resolved when updated to the new version. One last question, instead of rotating the labels I want to decrease the number of labels, so for eg show labels for every two weeks only.
Reply all
Reply to author
Forward
0 new messages