How to rescale raster layer in R?












1















I have a stacked raster layer of a world map that I am trying to rescale from -3.131791 to 3.387701 to 0 to 1. I followed this solution and the first method gives me max(x) = 1 but for some reason min(x) becomes 3.127125e-08. The second method gives me a range of 0 to 1 but when I plot it out my raster layer just becomes a grainy rectangle like this:
a grainy rectangle like this.



Here is my code:



## PCA ##
PCA1 <- rasterPCA(img=layers, nComp=1, spca=TRUE)

## Rescale 1 ##
rescale <- function(x, x.min = -3.131791, x.max = 3.387701, new.min = 1, new.max = 0) {
if(is.null(x.min)) {x.min = min(x)}
if(is.null(x.max)) {x.max = max(x)}
new.min + (x - x.min) * ((new.max - new.min) / (x.max - x.min))
}

PCAscaled1 <- calc(PCA1$map[[1]], rescale) #rescaled and inverted
plot(PCAscaled1)

## Rescale 2 ##
r <- PCAscaled1
r <- runif(ncell(PCAscaled1),-3.127125e-08,1)
r.min=cellStats(r,"min")
r.max=cellStats(r,"max")
PCAscaled1a <- (r-r.min)/(r.max-r.min)
plot(PCAscaled1a)


These are the parameters for the original raster in case it's relevant:



> PCA1$map[[1]]
class : RasterLayer
dimensions : 1352, 3570, 4826640 (nrow, ncol, ncell)
resolution : 0.1, 0.1 (x, y)
extent : -177, 180, -56.07567, 79.12433 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : /private/var/folders/3c/tsrm9qxs7t323jrz5k8h4d600000gn/T/RtmpKORCX5/raster/r_tmp_2018-11-17_002746_40497_32387.grd
names : PC1
values : -3.131791, 3.387701 (min, max)


I tried to see if any of the individual raster layers I used was the problem, and found out that all but one could be scaled to 0 to 1. But even when I exclude that raster layer I'm still not able to rescale it to 0 to 1. I also tried first rescaling all the individual raster layers then stacking them, but then the range of the stacked raster doesn't stay at 0 to 1 (I then tried to rescale that too but it still doesn't work).



If anyone knows what's wrong I'll really appreciate answers! If there is a different method of rescaling please let me know, I'll try that as well.










share|improve this question

























  • In Funtion rescale, you reversed max and min values:new.min should be 0, but you define as new.min=1. Neither do new.max.

    – Cobin
    Nov 24 '18 at 8:55











  • Thanks for your comment! This was intentional because I wanted to inverse the raster layer.

    – toffee
    Nov 25 '18 at 6:45
















1















I have a stacked raster layer of a world map that I am trying to rescale from -3.131791 to 3.387701 to 0 to 1. I followed this solution and the first method gives me max(x) = 1 but for some reason min(x) becomes 3.127125e-08. The second method gives me a range of 0 to 1 but when I plot it out my raster layer just becomes a grainy rectangle like this:
a grainy rectangle like this.



Here is my code:



## PCA ##
PCA1 <- rasterPCA(img=layers, nComp=1, spca=TRUE)

## Rescale 1 ##
rescale <- function(x, x.min = -3.131791, x.max = 3.387701, new.min = 1, new.max = 0) {
if(is.null(x.min)) {x.min = min(x)}
if(is.null(x.max)) {x.max = max(x)}
new.min + (x - x.min) * ((new.max - new.min) / (x.max - x.min))
}

PCAscaled1 <- calc(PCA1$map[[1]], rescale) #rescaled and inverted
plot(PCAscaled1)

## Rescale 2 ##
r <- PCAscaled1
r <- runif(ncell(PCAscaled1),-3.127125e-08,1)
r.min=cellStats(r,"min")
r.max=cellStats(r,"max")
PCAscaled1a <- (r-r.min)/(r.max-r.min)
plot(PCAscaled1a)


These are the parameters for the original raster in case it's relevant:



> PCA1$map[[1]]
class : RasterLayer
dimensions : 1352, 3570, 4826640 (nrow, ncol, ncell)
resolution : 0.1, 0.1 (x, y)
extent : -177, 180, -56.07567, 79.12433 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : /private/var/folders/3c/tsrm9qxs7t323jrz5k8h4d600000gn/T/RtmpKORCX5/raster/r_tmp_2018-11-17_002746_40497_32387.grd
names : PC1
values : -3.131791, 3.387701 (min, max)


I tried to see if any of the individual raster layers I used was the problem, and found out that all but one could be scaled to 0 to 1. But even when I exclude that raster layer I'm still not able to rescale it to 0 to 1. I also tried first rescaling all the individual raster layers then stacking them, but then the range of the stacked raster doesn't stay at 0 to 1 (I then tried to rescale that too but it still doesn't work).



If anyone knows what's wrong I'll really appreciate answers! If there is a different method of rescaling please let me know, I'll try that as well.










share|improve this question

























  • In Funtion rescale, you reversed max and min values:new.min should be 0, but you define as new.min=1. Neither do new.max.

    – Cobin
    Nov 24 '18 at 8:55











  • Thanks for your comment! This was intentional because I wanted to inverse the raster layer.

    – toffee
    Nov 25 '18 at 6:45














1












1








1








I have a stacked raster layer of a world map that I am trying to rescale from -3.131791 to 3.387701 to 0 to 1. I followed this solution and the first method gives me max(x) = 1 but for some reason min(x) becomes 3.127125e-08. The second method gives me a range of 0 to 1 but when I plot it out my raster layer just becomes a grainy rectangle like this:
a grainy rectangle like this.



Here is my code:



## PCA ##
PCA1 <- rasterPCA(img=layers, nComp=1, spca=TRUE)

## Rescale 1 ##
rescale <- function(x, x.min = -3.131791, x.max = 3.387701, new.min = 1, new.max = 0) {
if(is.null(x.min)) {x.min = min(x)}
if(is.null(x.max)) {x.max = max(x)}
new.min + (x - x.min) * ((new.max - new.min) / (x.max - x.min))
}

PCAscaled1 <- calc(PCA1$map[[1]], rescale) #rescaled and inverted
plot(PCAscaled1)

## Rescale 2 ##
r <- PCAscaled1
r <- runif(ncell(PCAscaled1),-3.127125e-08,1)
r.min=cellStats(r,"min")
r.max=cellStats(r,"max")
PCAscaled1a <- (r-r.min)/(r.max-r.min)
plot(PCAscaled1a)


These are the parameters for the original raster in case it's relevant:



> PCA1$map[[1]]
class : RasterLayer
dimensions : 1352, 3570, 4826640 (nrow, ncol, ncell)
resolution : 0.1, 0.1 (x, y)
extent : -177, 180, -56.07567, 79.12433 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : /private/var/folders/3c/tsrm9qxs7t323jrz5k8h4d600000gn/T/RtmpKORCX5/raster/r_tmp_2018-11-17_002746_40497_32387.grd
names : PC1
values : -3.131791, 3.387701 (min, max)


I tried to see if any of the individual raster layers I used was the problem, and found out that all but one could be scaled to 0 to 1. But even when I exclude that raster layer I'm still not able to rescale it to 0 to 1. I also tried first rescaling all the individual raster layers then stacking them, but then the range of the stacked raster doesn't stay at 0 to 1 (I then tried to rescale that too but it still doesn't work).



If anyone knows what's wrong I'll really appreciate answers! If there is a different method of rescaling please let me know, I'll try that as well.










share|improve this question
















I have a stacked raster layer of a world map that I am trying to rescale from -3.131791 to 3.387701 to 0 to 1. I followed this solution and the first method gives me max(x) = 1 but for some reason min(x) becomes 3.127125e-08. The second method gives me a range of 0 to 1 but when I plot it out my raster layer just becomes a grainy rectangle like this:
a grainy rectangle like this.



Here is my code:



## PCA ##
PCA1 <- rasterPCA(img=layers, nComp=1, spca=TRUE)

## Rescale 1 ##
rescale <- function(x, x.min = -3.131791, x.max = 3.387701, new.min = 1, new.max = 0) {
if(is.null(x.min)) {x.min = min(x)}
if(is.null(x.max)) {x.max = max(x)}
new.min + (x - x.min) * ((new.max - new.min) / (x.max - x.min))
}

PCAscaled1 <- calc(PCA1$map[[1]], rescale) #rescaled and inverted
plot(PCAscaled1)

## Rescale 2 ##
r <- PCAscaled1
r <- runif(ncell(PCAscaled1),-3.127125e-08,1)
r.min=cellStats(r,"min")
r.max=cellStats(r,"max")
PCAscaled1a <- (r-r.min)/(r.max-r.min)
plot(PCAscaled1a)


These are the parameters for the original raster in case it's relevant:



> PCA1$map[[1]]
class : RasterLayer
dimensions : 1352, 3570, 4826640 (nrow, ncol, ncell)
resolution : 0.1, 0.1 (x, y)
extent : -177, 180, -56.07567, 79.12433 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : /private/var/folders/3c/tsrm9qxs7t323jrz5k8h4d600000gn/T/RtmpKORCX5/raster/r_tmp_2018-11-17_002746_40497_32387.grd
names : PC1
values : -3.131791, 3.387701 (min, max)


I tried to see if any of the individual raster layers I used was the problem, and found out that all but one could be scaled to 0 to 1. But even when I exclude that raster layer I'm still not able to rescale it to 0 to 1. I also tried first rescaling all the individual raster layers then stacking them, but then the range of the stacked raster doesn't stay at 0 to 1 (I then tried to rescale that too but it still doesn't work).



If anyone knows what's wrong I'll really appreciate answers! If there is a different method of rescaling please let me know, I'll try that as well.







r raster






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 4:10







toffee

















asked Nov 24 '18 at 6:16









toffeetoffee

62




62













  • In Funtion rescale, you reversed max and min values:new.min should be 0, but you define as new.min=1. Neither do new.max.

    – Cobin
    Nov 24 '18 at 8:55











  • Thanks for your comment! This was intentional because I wanted to inverse the raster layer.

    – toffee
    Nov 25 '18 at 6:45



















  • In Funtion rescale, you reversed max and min values:new.min should be 0, but you define as new.min=1. Neither do new.max.

    – Cobin
    Nov 24 '18 at 8:55











  • Thanks for your comment! This was intentional because I wanted to inverse the raster layer.

    – toffee
    Nov 25 '18 at 6:45

















In Funtion rescale, you reversed max and min values:new.min should be 0, but you define as new.min=1. Neither do new.max.

– Cobin
Nov 24 '18 at 8:55





In Funtion rescale, you reversed max and min values:new.min should be 0, but you define as new.min=1. Neither do new.max.

– Cobin
Nov 24 '18 at 8:55













Thanks for your comment! This was intentional because I wanted to inverse the raster layer.

– toffee
Nov 25 '18 at 6:45





Thanks for your comment! This was intentional because I wanted to inverse the raster layer.

– toffee
Nov 25 '18 at 6:45












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455686%2fhow-to-rescale-raster-layer-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455686%2fhow-to-rescale-raster-layer-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'