[R-sig-Geo] Position of colorbar and color vs label breaks R raster

Zilefac Elvis zilefacelvis at yahoo.com
Sun Dec 14 00:02:12 CET 2014


Dear All,
I have a raster layer (lr) with the following attributes:

library(raster)

#-------------------------------------------------------
class       : RasterLayer 

dimensions  : 137, 229, 31373  (nrow, ncol, ncell) 
resolution  : 0.136, 0.0808  (x, y) 
extent      : -120.0072, -88.8632, 48.95328, 60.02288  (xmin, xmax, ymin, ymax) 
coord. ref. : +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs 
data source : in memory 
names       : myfile100 
values      : 106.6673, 172.4463  (min, max) 
#-----------------------------------------------------------

I write the raster to a directory like this:
writeRaster(r, file="myfile100.tif", format="GTiff", overwrite=TRUE) 

lr = raster("myfile100.tif")
bbox(lr) 
Long=c(-120.0072, -88.8632) 
Lat=c(48.95328, 60.02288)
brks <- seq(minValue(lr), maxValue(lr), by=10) 
nb <- length(brks)-1 
colors<-c("darkred", "red3","orange","gold1", "yellow", "lightskyblue","steelblue3", "royalblue3", 
"darkblue","darkblue") 


Long=c(-120.0072, -88.8632) 
Lat=c(48.95328, 60.02288)# plot the raster
plot(Long, Lat, type="n",axes=FALSE,xlab="",ylab="") 
plot(lr,add=TRUE,legend=F,axes=FALSE,lab.breaks=brks, zlim=c(105,175), 
main='first',col=colorRampPalette(c("darkred", "red3","orange","gold1", "yellow", "lightskyblue","steelblue3", "royalblue3", 
"darkblue","darkblue"))(30))# change number of colors 
#plot(Prairie.Boundaries,add=T)
 
1) Problem: Legend falls on the raster image and not out of the box. How can I change the postion of the raster? I found this answer but it just does not solve the problem. 
  
r.range <- c(minValue(lr), maxValue(lr)) 
plot(lr, legend.only=TRUE,legend.width=1.4, 
legend.shrink=1,col=colors, 
axis.args=list(at=seq(r.range[1], r.range[2], 10), 
labels=seq(r.range[1], r.range[2], 10), 
cex.axis=0.6), 
legend.args=list(text='Elevation (m)', side=4, font=2, line=2.5, cex=0.8)) 


2) How can I set set legend (colorbar) label breaks to match color breaks? 

I will send my raster upon request since I could not make a good reproducible example enough to illustrate the problem.

Many thanks,
Asong.



More information about the R-sig-Geo mailing list