[ACCEPTED]-R + ggplot2: how to hide missing dates from x-axis?-ggplot2
I made a package that does this. It's called bdscale
and 6 it's on CRAN and github. Shameless plug.
To replicate 5 your example:
> library(bdscale)
> library(ggplot2)
> library(scales)
> dts <- as.Date( c('2011-01-10', '2011-01-11', '2011-01-15', '2011-01-16'))
> ggplot(df, aes(x=dt, y=val)) + geom_point() +
scale_x_bd(business.dates=dts, labels=date_format('%d%b'))
But what you probably want 4 is to load known valid dates, then plot 3 your data using the valid dates on the x-axis:
> nyse <- bdscale::yahoo('SPY') # get valid dates from SPY prices
> dts <- as.Date('2011-01-10') + 1:10
> df <- data.frame(dt=dts, val=seq_along(dts))
> ggplot(df, aes(x=dt, y=val)) + geom_point() +
scale_x_bd(business.dates=nyse, labels=date_format('%d%b'), max.major.breaks=10)
Warning message:
Removed 3 rows containing missing values (geom_point).
The 2 warning is telling you that it removed three 1 dates:
- 15th = Saturday
- 16th = Sunday
- 17th = MLK Day
Turn the date data into a factor then. At 6 the moment, ggplot is interpreting the data 5 in the sense you have told it the data are 4 in - a continuous date scale. You don't 3 want that scale, you want a categorical 2 scale:
require(ggplot2)
dts <- as.Date( c('2011-01-10', '2011-01-11', '2011-01-15', '2011-01-16'))
df <- data.frame(dt = dts, val = seq_along(dts))
ggplot(df, aes(dt,val)) + geom_point() +
scale_x_date(format = '%d%b', major='days')
versus
df <- data.frame(dt = factor(format(dts, format = '%d%b')),
val = seq_along(dts))
ggplot(df, aes(dt,val)) + geom_point()
which produces:
Is that what 1 you wanted?
First question is : why do you want to do 11 that? There is no point in showing a coordinate-based 10 plot if your axes are not coordinates. If 9 you really want to do this, you can convert 8 to a factor. Be careful for the order though 7 :
dts <- c(as.Date( c('31-10-2011', '01-11-2011', '02-11-2011',
'05-11-2011'),format="%d-%m-%Y"))
dtsf <- format(dts, format= '%d%b')
df <- data.frame(dt=ordered(dtsf,levels=dtsf),val=seq_along(dts))
ggplot(df, aes(dt,val)) + geom_point()
With factors you have to be careful, as 6 the order is arbitrary in a factor,unless 5 you make it an ordered factor. As factors 4 are ordered alphabetically by default, you 3 can get in trouble with some date formats. So 2 be careful what you do. If you don't take 1 the order into account, you get :
df <- data.frame(dt=factor(dtsf),val=seq_along(dts))
ggplot(df, aes(dt,val)) + geom_point()
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.