5

I am using the following ggplot2 v0.9 scale_x_continious logic in a loop (by county name) in an attempt to plot each county's data on a seperate plot with identical x scales.

MaxDays=365*3;
p <- p + scale_x_continuous(limits=c(0,MaxDays))
p <- p + scale_x_continuous(breaks=seq(0,MaxDays,60))

The logic works great, if all counties have data >= MaxDate. However, if the number of days is less than the MaxDate the charts x scales are not uniform (i.e say 0 - 720 days)

How can set the scalse to be absolute instead of a limit?

Any assistance woudl be greatly appreciated

############################################
###  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()
MikeTP
  • 7,716
  • 16
  • 44
  • 57

1 Answers1

9
p <- p + coord_cartesian(xlim=c(0, MaxDays))

EDIT: based on comments.

Your problem is that the second scale_x_continuous() is replacing, not augmenting, the first, so the limits are not kept.

You can replace the lines

p <- p + scale_x_continuous(limits=c(xrng[1],xrng[2])) 
p <- p + scale_x_continuous(breaks=seq(xrng[1],xrng[2],1))

with

p <- p + scale_x_continuous(limits=c(xrng[1],xrng[2]), 
                            breaks=seq(xrng[1],xrng[2],1))

which gives something like this:

enter image description here

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
  • Thanks but...do i need to add a package? Error: could not find function "corrd_cartesian" – MikeTP Mar 19 '12 at 20:15
  • No, I had a typo which @joran was kind enough to fix. – Brian Diggs Mar 19 '12 at 20:22
  • Thank you. Now I don't return error but still not what I was trying to accomplish. Looking for all my x and y axis (and ticks) to be the same even if there the data for a particular plot in the loop dosent justify a such a high level for the maximum x axis. Basicly the equivilent of the default scales in facet_grid except I want a full size plot for each county. – MikeTP Mar 19 '12 at 20:46
  • This was meant to be in addition to your `scale_x_continuous(breaks=...)` line, not in place of it. – Brian Diggs Mar 19 '12 at 20:53
  • I added some sample data/code above to my origional post. Hopefully this will help better articulate my question. Sorry for the verbose code but if you look at page 2 of the pdf plots you will see that the x axis stops where the data stops. I woudl like it to be uniform with what is shown on page 1 even if their is no data. – MikeTP Mar 19 '12 at 21:46
  • @BrianDiggs I've seen Hadley saying that we should not use '$' out of ggplot, e.g., scale_x_continuous(limits=c(min(df3$Days),max(df3$Days))). Is the "xrng=range(df3$Days)" approach the best one? Thanks. – Eduardo May 31 '13 at 12:27
  • 1
    @Eduardo Accessing columns of a data frame using `$` (or even `[[`) in aesthetic mappings and transformations is not appropriate. For something like the limits of scales, it is less of a problem; assigning the result of the computation to a variable outside the `ggplot` call is probably better but should be equivalent in functionality. If this doesn't answer your question well enough, post a new question. – Brian Diggs May 31 '13 at 17:38