r - Error when changing axis format with scale_x_date -
dear fellow homo sapiens,
i've created this graph using code below (except without + scale_x_date
).
the data-set i've used found here. looks this:
gfc1cari<-read.csv("demo.csv") head(gfc1cari) x xx variable value var 1 1 2008-07-17 financials 0.001772705 financ 2 2 2008-07-18 financials 0.017306086 financ 3 3 2008-07-21 financials 0.010745136 financ 4 4 2008-07-22 financials 0.021152194 financ 5 5 2008-07-23 financials 0.031195805 financ 6 6 2008-07-24 financials 0.026534444 financ
as you'll see in picture, x-axis displays dates such "aug", "sep" .... . however, want similar "08/2007", "09/2007" ... . so, attempt @ doing displayed below, doesn't work.
p <- ggplot(data=gfc1cari,aes(x=xx, y=value, colour=var)) + geom_line() + opts(legend.position = "none") + xlab(" ") + ylab("cumulative abnormal return") + scale_x_date(labels = date_format("%m/%y"),breaks = "1 month") p <- direct.label(p+xlim(min(gfc1cari$xx), max(gfc1cari$xx)+20))
i've tried many other variation of breaks = ??
, date_format
, can't %m/%y show, it'll "aug" .. "sep" .. "oct". requested joran:
> str(gfc1cari) 'data.frame': 1730 obs. of 5 variables: $ x : int 1 2 3 4 5 6 7 8 9 10 ... $ xx : posixct, format: "2008-07-17" "2008-07-18" "2008-07-21" "2008-07-22" ... $ variable: factor w/ 10 levels "financials","industrials",..: 1 1 1 1 1 1 1 1 1 1 ... $ value : num 0.00177 0.01731 0.01075 0.02115 0.0312 ... $ var : chr "financ" "financ" "financ" "financ" ...
i've tried change date formate posixct
as.date
follows:
gfc1cari[,2]<-as.date(gfc1cari[,2]) str(gfc1cari) 'data.frame': 1730 obs. of 5 variables: $ x : int 1 2 3 4 5 6 7 8 9 10 ... $ xx : date, format: "2008-07-16" "2008-07-17" ... $ variable: factor w/ 10 levels "financials","industrials",..: 1 1 1 1 1 1 1 1 1 1 ... $ value : num 0.000983 0.015822 0.008339 0.017987 0.027186 ... $ var : chr "financ" "financ" "financ" "financ" ...
this doesn't fix situation. instead x-axis { "oct", "jan", "apr" }, , that's it.
thanks everyone.
my apologies, being dense. thought error getting happening before attempt use directlabels
, not after.
the call xlim
stomping on x axis formatting. put scale_x_date
call inside direct.labels
command works me:
p <- direct.label(p + xlim(min(dat$xx), max(dat$xx)+20) + scale_x_date(labels = date_format("%m/%y"),breaks = "1 month"))
Comments
Post a Comment