scale - Controlling legend and colors for raster values in R (equivalent to Matlab's caxis)? -


i'm trying plot esri grid raster image of surface. i've figured out how make plot, not how control r's color scale.

# open necessary libraries library("raster", lib.loc="c:/users/sam/documents/r/win-library/2.15") library("rgdal", lib.loc="c:/users/sam/documents/r/win-library/2.15") library("ncdf", lib.loc="c:/users/sam/documents/r/win-library/2.15")  # goal: select esri grid ascii file , plot image. infile <- file.choose("c:/users/sam/dropbox/wsc/class stuff/groundwater hydraulics/swb wibu/results") r <- raster(infile)  # read in metadata esri output file, split relevant variables info <- read.table(infile, nrows=6)  ncols <- info[1,2] nrows <- info[2,2] xllcorner <- info[3,2] yllcorner <- info[4,2] cellsize <- info[5,2] nodata_value <- info[6,2] xurcorner <- xllcorner+(ncols-1)*cellsize yurcorner <- yllcorner+(nrows-1)*cellsize  # plot output data - whole model domain pal <- colorramppalette(c("purple","blue","cyan","green","yellow","red")) par(mar = c(5,5,2,4))  # margins each plot, room colorbars par(pin=c(5,5))  # set size of plots (inches) par(xaxs="i", yaxs="i")  # set axes fit data plotted plot(r, xlim=c(xllcorner, xurcorner), ylim=c(yllcorner, yurcorner), ylab='utm zone 16 n northing [m]', xlab='utm zone 16 n easting [m]', col = pal(50)) 

an example of 'infile' this:

ncols        262   nrows        257   xllcorner     304055.000   yllcorner    4792625.000   cellsize         10.000   nodata_value    -9999.000   42.4 42.6 42.2 0 42.2 42.8 40.5 40.5 42.5 42.5 42.5 42.9 43.0 ...   42.5 42.5 42.5 0 0 43.3 42.7 43.0 40.5 42.5 42.5 42.4 41.9 ...   42.2 42.7 41.9 42.9 0 0 43.7 44.0 42.4 42.5 42.5 43.3 43.2 ...   42.5 42.5 42.5 42.5 0 0 41.9 40.5 42.4 42.5 42.4 42.4 40.5 ...   41.9 42.9 40.5 43.3 40.5 0 41.9 42.8 42.4 42.4 42.5 42.5 42.5 ...   ...   

the problem 0 values in data stretch color axis beyond what's useful me. can't post image because don't have enough reputation points, example here: http://i.stack.imgur.com/osde0.png

enter image description here

basically, tell r stretch color axis 25-45, rather 0-50. know in matlab use command caxis. r have similar?

i used dataset "volcano" generate raster object, , make code reproducible.

here 2 options can try:

1- plot

library(raster) library(rcolorbrewer)  r = raster(volcano) #raster object  cuts=c(100,150,160,170,180,190,200) #set breaks pal <- colorramppalette(c("white","black"))  plot(r, breaks=cuts, col = pal(7)) #plot defined breaks 

enter image description here

2- ggplot

library(ggplot2) library(raster) library(rcolorbrewer)  r = raster(volcano) #raster object #preparing raster object plot geom_tile in ggplot2 r_points = rastertopoints(r) r_df = data.frame(r_points) head(r_df) #breaks set column "layer" r_df$cuts=cut(r_df$layer,breaks=c(100,150,160,170,180,190,200)) #set breaks  ggplot(data=r_df) +    geom_tile(aes(x=x,y=y,fill=cuts)) +    scale_fill_brewer("legend_title",type = "seq", palette = "greys") +   coord_equal() +   theme_bw() +   theme(panel.grid.major = element_blank()) +   xlab("longitude") + ylab("latitude") 

enter image description here


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -