[This article was first published on Misanthrope's Thoughts, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The issue with the broken histogram creation tool in QGIS annoyed me far too long. Sometimes you just need a quick glance on the histogram of a raster just to make a decision on how to process it or just to assess distribution of classes. But as you know, QGIS histogram tool creates broken plots: they are just useless (don’t know if something have changed in Master).
Fortunately there is the SEXTANTE – extremely useful plugin that provides access to hundreds of geoprocessing algorithms and allows to create your own very quickly. SEXTANTE grants access to R functions, and one of the out-of-the-box example scripts, surprise-surprise, is about raster histogram creation. “Hooray! The problem is solved!” – one may cry out. Unfortunately it’s not. “Raster histogram” tool is somewhat suitable only for single-band rasters (its algorithm is as simple as hist(as.matrix(raster))). Obviously we need to create our own tool. Luckily it is extremely simple if one is already familiar with R.
To create your own R tool for QGIS go to Analisys menu and activate SEXTANTE toolbox. Then go to R-scripts -> Tools and double-click ‘Create new R script’ and just copy and paste this code (an explanatoin will follow the description):
To create your own R tool for QGIS go to Analisys menu and activate SEXTANTE toolbox. Then go to R-scripts -> Tools and double-click ‘Create new R script’ and just copy and paste this code (an explanatoin will follow the description):
##layer=raster ##Tools=group ##dens_or_hist=string ##showplots library('rpanel') library('rasterVis') str <- dens_or_hist if (str !='dens' & str != 'hist'){ rp.messagebox('you must enter "dens" or "hist"', title = 'oops!') } else { im <- stack(layer) if (str == 'dens') { densityplot(im) } else if (str == 'hist') { histogram(im) } }Now save the script. It will appear among other R scripts. Launch it and you will see this window:
Script window |
If you chose density plot you will get something like this:
Density plot |
Histogram |
Fail window |
How it works.
##layer=raster provides us with the drop-down menu to select needed raster and it will be addresed as layer in our script.##Tools=group will assign our script to the Tools group of R scripts in SEXTANTE.
##dens_or_hist=string provides us with the string input field dens or hist.
##showplots will show the plot in Results window after execution.
library(‘rpanel’) this library is needed to show pop-up window with the error when the user fails to type 4 letters correctly.
library(‘rasterVis’) this library works on top of raster package and it is responsible for plotting our graphs.
Then we define condition to pop up error window via rp.messagebox if the plot type is misspelled or missing.
Finally, we transform our raster to the RasterStack object to be able to run either densityplot() or histogram() function.