ggplot2 - set axis breaks in R with POSIX time -


i have 2 line graphs created dataset https://www.dropbox.com/s/77epslia52odt7m/undervotes_new.csv

they graphs of cumulative votes based on type of ballot cast. wanted use ggplot facets create cumulative vote graphs based on type of ballot cast. royally screwed la problem not figure out how fix: why `cumsum` doesn't work within groups or facets in ggplot?

so, work around plot them separately , combine gridarrange. axis differ in breakpoints. know can set manually, having trouble doing because in posix format?

how can set breaks (and ideally range) on both graphs "nov 06, nov 07, nov 08", etc etc. on both graphs?

thanks,

library(reshape2) library(ggplot2) library(gridextra) library(grid)  #transform variables data$net<-as.numeric(as.character(data$net)) data$createdate<-strptime(as.character(data$createdate), "%m/%d/%y %h:%m") data$createdate<-as.posixlt(data$createdate)  #get rid of nasty nas data<-data[complete.cases(data[,c(15)]),]  ##subset candidate datasubobama<-data[data$ballotname=="barack obama",] datasubromney<-data[data$ballotname=="mitt romney",]  ##order date datasubobama<-datasubobama[order(datasubobama$createdate),] #order date datasubromney<-datasubromney[order(datasubromney$createdate),] #order date  ##get rid of outliers datasubobama<-datasubobama[1:380,] datasubromney<-datasubromney[1:345,]  ##subset types of votes datasubobamac<-datasubobama[datasubobama$resultstype=="certified votes",] datasubobamap<-datasubobama[datasubobama$resultstype=="provisional votes counted",] datasubromneyc<-datasubromney[datasubromney$resultstype=="certified votes",] datasubromneyp<-datasubromney[datasubromney$resultstype=="provisional votes counted",]  ####this obama/romney certified votes cumsumc<-ggplot(datasubobamac, aes(x=as.posixlt(datasubobamac$createdate),  y=cumsum(datasubobamac$net))) cumsumc<-cumsumc+geom_line(color="blue") cumsumc<-cumsumc+geom_point(color="black") cumsumc<-cumsumc+geom_line(data=datasubromneyc, color="red",     aes(x=as.posixlt(datasubromneyc$createdate), y=cumsum(datasubromneyc$net))) cumsumc<-cumsumc+geom_point(data=datasubromneyc,color="black",     aes(x=as.posixlt(datasubromneyc$createdate), y=cumsum(datasubromneyc$net))) cumsumc<-cumsumc+ggtitle("obama (blue) , romney (red) cumulative sum [certified]") cumsumc<-cumsumc+xlab("date") cumsumc<-cumsumc+ylab("net votes") cumsumc<-cumsumc+theme(strip.text.y = element_text(size = 20, color="black")) cumsumc<-cumsumc+theme(plot.title=element_text(size=20)) cumsumc<-cumsumc+theme(axis.title.x = element_text(size=20)) cumsumc<-cumsumc+theme(axis.title.y = element_text(size=20, vjust=1.5,)) cumsumc<-cumsumc+theme(axis.text.x=element_text(size=15)) cumsumc<-cumsumc+theme(axis.text.y=element_text(size=15)) cumsumc<-cumsumc+theme(axis.ticks.margin=unit(c(.05,.05),'cm')) cumsumc<-cumsumc+theme(plot.margin=unit(c(.3,1,.3,1),"cm")) cumsumc  #this same provisional cumsump<-ggplot(datasubobamap, aes(x=as.posixlt(datasubobamap$createdate),     y=cumsum(datasubobamap$net))) cumsump<-cumsump+geom_line(color="blue") cumsump<-cumsump+geom_point(color="black") cumsump<-cumsump+geom_line(data=datasubromneyp, color="red",      aes(x=as.posixlt(datasubromneyp$createdate), y=cumsum(datasubromneyp$net))) cumsump<-cumsump+geom_point(data=datasubromneyp,color="black",         aes(x=as.posixlt(datasubromneyp$createdate), y=cumsum(datasubromneyp$net))) cumsump<-cumsump+ggtitle("obama (blue) , romney (red) cumulative sum [provisional]") cumsump<-cumsump+xlab("date") cumsump<-cumsump+ylab("net votes") cumsump<-cumsump+theme(strip.text.y = element_text(size = 20, color="black")) cumsump<-cumsump+theme(plot.title=element_text(size=20)) cumsump<-cumsump+theme(axis.title.x = element_text(size=20)) cumsump<-cumsump+theme(axis.title.y = element_text(size=20, vjust=1.5,)) cumsump<-cumsump+theme(axis.text.x=element_text(size=15)) cumsump<-cumsump+theme(axis.text.y=element_text(size=15)) cumsump<-cumsump+theme(axis.ticks.margin=unit(c(.05,.05),'cm')) cumsump<-cumsump+theme(plot.margin=unit(c(.3,1,.3,1),"cm")) cumsump  gridcumsum<-grid.arrange(cumsumc,cumsump) 

guess what, found answer you: want using

scale_x_datetime

http://docs.ggplot2.org/0.9.3.1/scale_datetime.html

it's super neat , want. know new r, it's ok. luck!

thanks , great job finding this.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -