[R] ggplo2: fixed extent greater than data?

thegeologician kmr at thegeologician.net
Mon Nov 26 22:26:46 CET 2007



hadley wrote:
> 
> On 11/23/07, thegeologician <kmr at thegeologician.net> wrote:
> 
>> I'd like to produce a series of maps with different data on, but exactly
>> the
>> same extent in each plot. Is there a way of switching the automatic
>> extent
>> (to the data of the last layer added) OFF? I'm trying something like:
>>
> 
> I think this is a bug in coord_map, as the limits set on the scales
> are basically  ignored.  However, I'm not completely sure how to fix
> this, as simply subsetting the data to be contained within those
> bounds may drop off points that are necessary to correctly draw
> boundaries.
> 
> Essentially the problem is that the limits are specified on the
> unprojected data, and I don't know how to apply them to the projected
> data (and you might not want to do that anyway, given that it could
> produce non-linear boundaries).
> 

Yes, that's true, due to the projection. The coordinate grid will "bulge" in
or out of the (projected) rectangular area defined by its corners. But in my
case it doesn't matter, as long as I can get all maps to show the same
extent... Anyway, it might be a nice touch, if at some point you could get
that to work too - but we're talking GIS here, might be a bit far out of
what R is basically designed for... Surely not a top issue for ggplot2.


hadley wrote:
> 
>> Additional question: is there a way of eliminating the extra spacing in a
>> map projection? The "expand=c(0,0)" parameter seems not to work...
> 
> This is a bug.  The fix will be included in the next version of
> ggplot, or you can fix the current version by running this code:
> 
> ScaleContinuous$new <- function(., name=NULL, limits=c(NA,NA),
> breaks=NULL, labels=NULL, variable, trans="identity", expand=c(0.05,
> 0)) {
>    if (is.null(breaks) && !is.null(labels)) stop("Labels can only be
> specified in conjunction with breaks")
> 
>    .$proto(name=name, .input=variable, .output=variable,
> limits=limits, .breaks = breaks, .labels = labels, .expand=expand)
>  }
> 

Thanks, that did fix THAT particular problem, indeed. But I encountered a
few new ones..:

At the moment, I'm doing something like:

getOverviewMap<-function(xmin=4,xmax=18,ymin=43,ymax=50,proj="",par=NULL){
	p2<-ggplot()
	p2<-p2+geom_path(data=wa,mapping=aes(x=x,y=y))	# political boundaries
	p2<-p2+geom_point(data=spts,mapping=aes(x=Lon,y=Lat,colour="red"))	# sample
localities
	
	# dummy points to fix extent:
	xb<-c(xmin,xmax,xmin,xmax)
	yb<-c(ymin,ymin,ymax,ymax)
	bd<-data.frame(xb,yb)
	p2<-p2+geom_point(data=bd,mapping=aes(x=xb,y=yb,plot=FALSE))
	
	#axes:

p2<-p2+scale_x_continuous(limits=c(xmin,xmax),expand=c(0,0),breaks=c(5,10,15,20))

p2<-p2+scale_y_continuous(limits=c(ymin,ymax),expand=c(0,0),breaks=c(42,44,46,48,50))
	
	p2$legend<-FALSE
	if(!(proj=="none")) p2<-p2+coord_map(project=proj,parameters=par)
	return(p2)
}

I've fixed the extent-problem with some dummy points, outside of the area of
scope. The plot zooms to them, since I add them last.
"wa" is extracted from a call to "map()", as shown on the ggplot2 site.
"spts" holds the sample localities.

Problem 1 (probably my stupidity): when I plot the sample localities without
"colour=", they will be black and without Legend. When I give them colour,
as above, I can not get rid of the Legend! I tried to subset the plot and
gave the "..$legend<-FALSE" to different subsets and/or all layers, tried
the above statement at several places in the function... I didn't find the
trick. ;-(

Problem 2 (definitely my stupidity): I even couldn't find the right position
to place the "plot=FALSE" parameter, to suppress plotting of the dummy
points (corners). Or would ggplot2 "notice" and revert to the unwanted
smaller extent? Can I give them "invisible" colour?

Problem 3 (bug?): for testing, the function is designed to allow for the map
projection to be passed on. In cartesian coordinates ("proj="none"")
everything works fine. When I switch to - any, even rectangular - map
projection, I don't know how to override coord_map()s default behaviour to
omit the tick marks!?

Problem 4 (bug): As I said, cartesian coords are fine. In a map projection,
the country boundaries jump to the east by ~1 degree! It seems to me, "wa" -
but not the points? - might get "projected twice", because of this strange
behaviour: when the function is run with "proj="none"" it works fine
(default is cartesian). Any map projection set, the countries are offset.
BUT: When called with "proj=""" after that, the map will also be fine (AND
projected)!! Since the parameter is passed through to mapproject, and there
it says an empty "projection=""" defaults to the last projection used, I
assume no new calculation occurs in this case. The same happens even with
mercator projection (coord_map()s default) given as parameter.

Sorry for the lengthy post, but maybe my descriptions can help to track the
bugs... And of course I'd be thankful for any help! ;-)


-- 
View this message in context: http://www.nabble.com/ggplo2%3A-fixed-extent-greater-than-data--tf4863198.html#a13959038
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list