1

Looking to the tutorial in http://bleutner.github.io/RStoolbox/rstbx-docu/rasterPCA.html, in which they show how to do a Principal Component Analysis on raster data, I was wandering whether is it possible to convert the ggRGB plot in a raster file.

Do you have any suggestion?

1 Answer 1

2

Slot map inside PCA result is a RasterBrick:

library(RStoolbox)
library(ggplot2)
library(reshape2)

data(rlogo)

set.seed(25)

rpc <- rasterPCA(rlogo)

class(rpc$map)

[1] "RasterBrick"
attr(,"package")
[1] "raster"

You can save this slot as a raster with raster package:

library(raster)

writeRaster(rpc$map, "rpc.tif")

You can set bylayer = FALSE if you want a multi-layer raster as output, default is TRUE.

Also, read function description:

Principal Component Analysis for Rasters

Description
Calculates R-mode PCA for RasterBricks or RasterStacks and returns a RasterBrick with multiple layers of PCA scores.

Not the answer you're looking for? Browse other questions tagged or ask your own question.