用POSIX时间设置R中的轴断点

时间:2014-08-18 15:16:14

标签: r ggplot2 axis-labels

我有两个从此数据集https://www.dropbox.com/s/77epslia52odt7m/undervotes_new.csv

创建的折线图

它们是基于投票类型的累积投票图。我想使用ggplot facets根据选票类型创建累积投票图。但是,这个问题很糟糕,我无法弄清楚如何解决这个问题:Why `cumsum` doesn't work within groups or facets in ggplot?

所以,我的工作只是将它们分开绘制并与gridarrange结合使用。但轴的断点不同。我知道他们可以手动设置,但我这样做是因为它们是POSIX格式吗?

如何在两个图表上设置两个图表上的中断(以及理想情况下的范围)为" 11月06日,11月07日,11月08日和#34;等等?

谢谢,

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)

1 个答案:

答案 0 :(得分:1)

猜猜看,我找到了答案: 你想要使用

scale_x_datetime

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

它非常整洁,完全符合您的要求。但我知道你是R的新手,所以没关系。祝你好运!

非常感谢你找到了这个。