0

I have two line graphs created from this dataset https://www.dropbox.com/s/77epslia52odt7m/undervotes_new.csv

They are graphs of cumulative votes based on what type of ballot was cast. I wanted to use ggplot facets to create the cumulative vote graphs based on type of ballot cast. But that royally screwed up a la this problem which I could not figure out how to fix: Why `cumsum` doesn't work within groups or facets in ggplot?

So, my work around is just to plot them separately and combine with gridarrange. But the axis differ in their breakpoints. I know they can be set manually, but I am having trouble doing this because they are in POSIX format?

How can I set the breaks (and ideally the range) on both graphs to be "Nov 06, Nov 07, Nov 08", etc etc. on both graphs?

Thanks,

library(reshape2)
library(ggplot2)
library(gridExtra)
library(grid)

#transform some 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 by candidate
Datasubobama<-Data[Data$BallotName=="Barack Obama",]
Datasubromney<-Data[Data$BallotName=="Mitt Romney",]

##Order by date
Datasubobama<-Datasubobama[order(Datasubobama$CreateDate),] #order by date
Datasubromney<-Datasubromney[order(Datasubromney$CreateDate),] #order by date

##Get rid of outliers
Datasubobama<-Datasubobama[1:380,]
Datasubromney<-Datasubromney[1:345,]

##Subset into 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 is obama/romney certified votes only
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) and 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 is the same for Provisional Only
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) and 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)
Community
  • 1
  • 1
PSQ
  • 11
  • 2
  • There is no need for `grid.arrange`. Just use one of the split-apply-combine functions (`ddply` from package plyr is easy to use) to calculate the cumsums groupwise and then facet the plot. That takes care of your axis problem and produces a nicer looking graph. – Roland Aug 18 '14 at 15:22
  • dont you get tired of typing cumsumP <- cumsumP + ... ? – rawr Aug 18 '14 at 16:25
  • @Roland, forgive my ignorance, but every time I tried a solution to the faceting issue, it didn't work. could you give a little taste of what doing what your suggestions would look like? – PSQ Aug 18 '14 at 16:37
  • Create a [minimal reproducible example](http://stackoverflow.com/a/5963610/1412059) and I might bother. – Roland Aug 18 '14 at 16:41
  • @rawr, i do. i really do. just a bad habit i suppose. – PSQ Aug 18 '14 at 16:41
  • @Roland, is providing the data and the code not enough? – PSQ Aug 18 '14 at 16:46
  • You seem to not understand the word "minimal" and I don't like extracting data from some dropbox. – Roland Aug 18 '14 at 16:50
  • @Roland Not that I want to get into an argument with a stranger on the internet, but you don't do a lot to welcome new people to R. Maybe instead, you could just tell me how to use scale_y_continuous(breaks=(c("Sep. 6, Sep. 7, etc"))) to accomplish what I need, in a way that makes sense with my level of understanding of R. Thanks. – PSQ Aug 18 '14 at 16:55
  • @PSQ I (and others) answer questions here free of charge in my free time. If you make it harder to answer your question by, e.g., including a lot of code, which is not directly related to the problem (what does the `theme` or NA handling or outlier removal matter here), I'll go and answer some other question instead. Being able to create a minimal reproducible example is a very useful skill. Quite often, you'll find the solution yourself in the process of creating one. Anyway, if you want answers, you should it make as easy as possible to answer. – Roland Aug 19 '14 at 07:09

1 Answers1

1

Guess what, I found an answer for you: You want to be using

scale_x_datetime

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

It's super neat and does exactly what you want. But I know you are new to R, so it's ok. Good luck!

Thanks and great job finding this.

PSQ
  • 11
  • 2