我在一个循环(按县名)中使用以下ggplot2 v0.9 scale_x_continious逻辑,试图在具有相同x标度的单独图上绘制每个县的数据。
MaxDays=365*3;
p <- p + scale_x_continuous(limits=c(0,MaxDays))
p <- p + scale_x_continuous(breaks=seq(0,MaxDays,60))
如果所有县都有数据&gt; = MaxDate,那么逻辑很有效。但是,如果天数小于MaxDate,则图表x标度不均匀(即0 - 720天)
如何将scalse设置为绝对值而不是限制?
非常感谢任何帮助
############################################
### Sample Data Below
############################################
# County 1 data
Days=seq(1,30,1)
Qty=Days*10
County=rep("Washington",length(Days))
df1=data.frame(County, Qty, Days)
# County 2 data
Days=seq(1,15,1)
Qty=Days*20
County=rep("Jefferson",length(Days))
df2=data.frame(County, Qty, Days)
# County 1 and 2 data
df3=rbind(df1,df2)
# calculate ranges for x scales
yrng=range(df3$Qty)
xrng=range(df3$Days)
# Scatter Plots
fname=paste("C:/test",".pdf",sep="");
pdf(fname,10,8,onefile=TRUE,paper="a4r");
p <- ggplot()
cnty=unique(df3$County)
n=length(unique(df3$County))
for (i in 1:n){
df4<-subset(df3, County==cnty[i])
p <- ggplot(df4, aes(x=Days, y=Qty))
p <- p + geom_point()
p <- p + opts(title=cnty[i])
p <- p + scale_x_continuous(limits=c(xrng[1],xrng[2]))
p <- p + scale_x_continuous(breaks=seq(xrng[1],xrng[2],1))
p <- p + coord_cartesian(xlim=c(xrng[1],xrng[2]))
print(p);
}
dev.off()
答案 0 :(得分:9)
p <- p + coord_cartesian(xlim=c(0, MaxDays))
编辑:基于评论。
你的问题是第二个scale_x_continuous()
正在替换第一个p <- p + scale_x_continuous(limits=c(xrng[1],xrng[2]))
p <- p + scale_x_continuous(breaks=seq(xrng[1],xrng[2],1))
,而不是扩充,因此不会保留限制。
您可以替换
行p <- p + scale_x_continuous(limits=c(xrng[1],xrng[2]),
breaks=seq(xrng[1],xrng[2],1))
与
{{1}}
这样的东西: