plot shapefile variable: Error invalid gray level, must be in [0,1]. R











up vote
0
down vote

favorite












I have a shapefile which I have run through a spatial regression model in R.



I have pulled out the residuals from the regression model and added them as a new variable to the original shapefile. I am hoping to map these residuals but am encountering some issues.



Here is my code:



     plot(shapefile,col=gray(shapefile@data$residuals))


I am receiving this error:



  invalid gray level, must be in [0,1].


Am I correct in assuming this is to do with the values of the residuals? They span from -40 to +20. Is there a way to change this scale from 0-1 to a larger scale? or an easier way to plot these please?



I have also tried spplot:



     spplot(shape@data$residuals)


and receive the error:



 Error in (function (classes, fdef, mtable)  : 
unable to find an inherited method for function ‘spplot’ for signature
‘"numeric"’









share|improve this question






















  • Aye the gray() function needs the first parameter to be [0,1] bounded. You can use scales::rescale() to scale your [-40,20] to [0,1]
    – hrbrmstr
    Nov 11 at 17:13










  • Unless you are printing in black and white, its usually better to plot residuals with a diverging palette so 0 is a neutral colour, and maybe reds are +ve and blues are -ve. You might also look at the tmap package for mapping spatial data.
    – Spacedman
    Nov 11 at 17:29















up vote
0
down vote

favorite












I have a shapefile which I have run through a spatial regression model in R.



I have pulled out the residuals from the regression model and added them as a new variable to the original shapefile. I am hoping to map these residuals but am encountering some issues.



Here is my code:



     plot(shapefile,col=gray(shapefile@data$residuals))


I am receiving this error:



  invalid gray level, must be in [0,1].


Am I correct in assuming this is to do with the values of the residuals? They span from -40 to +20. Is there a way to change this scale from 0-1 to a larger scale? or an easier way to plot these please?



I have also tried spplot:



     spplot(shape@data$residuals)


and receive the error:



 Error in (function (classes, fdef, mtable)  : 
unable to find an inherited method for function ‘spplot’ for signature
‘"numeric"’









share|improve this question






















  • Aye the gray() function needs the first parameter to be [0,1] bounded. You can use scales::rescale() to scale your [-40,20] to [0,1]
    – hrbrmstr
    Nov 11 at 17:13










  • Unless you are printing in black and white, its usually better to plot residuals with a diverging palette so 0 is a neutral colour, and maybe reds are +ve and blues are -ve. You might also look at the tmap package for mapping spatial data.
    – Spacedman
    Nov 11 at 17:29













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a shapefile which I have run through a spatial regression model in R.



I have pulled out the residuals from the regression model and added them as a new variable to the original shapefile. I am hoping to map these residuals but am encountering some issues.



Here is my code:



     plot(shapefile,col=gray(shapefile@data$residuals))


I am receiving this error:



  invalid gray level, must be in [0,1].


Am I correct in assuming this is to do with the values of the residuals? They span from -40 to +20. Is there a way to change this scale from 0-1 to a larger scale? or an easier way to plot these please?



I have also tried spplot:



     spplot(shape@data$residuals)


and receive the error:



 Error in (function (classes, fdef, mtable)  : 
unable to find an inherited method for function ‘spplot’ for signature
‘"numeric"’









share|improve this question













I have a shapefile which I have run through a spatial regression model in R.



I have pulled out the residuals from the regression model and added them as a new variable to the original shapefile. I am hoping to map these residuals but am encountering some issues.



Here is my code:



     plot(shapefile,col=gray(shapefile@data$residuals))


I am receiving this error:



  invalid gray level, must be in [0,1].


Am I correct in assuming this is to do with the values of the residuals? They span from -40 to +20. Is there a way to change this scale from 0-1 to a larger scale? or an easier way to plot these please?



I have also tried spplot:



     spplot(shape@data$residuals)


and receive the error:



 Error in (function (classes, fdef, mtable)  : 
unable to find an inherited method for function ‘spplot’ for signature
‘"numeric"’






r regression






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 16:35









Will.S89

857




857












  • Aye the gray() function needs the first parameter to be [0,1] bounded. You can use scales::rescale() to scale your [-40,20] to [0,1]
    – hrbrmstr
    Nov 11 at 17:13










  • Unless you are printing in black and white, its usually better to plot residuals with a diverging palette so 0 is a neutral colour, and maybe reds are +ve and blues are -ve. You might also look at the tmap package for mapping spatial data.
    – Spacedman
    Nov 11 at 17:29


















  • Aye the gray() function needs the first parameter to be [0,1] bounded. You can use scales::rescale() to scale your [-40,20] to [0,1]
    – hrbrmstr
    Nov 11 at 17:13










  • Unless you are printing in black and white, its usually better to plot residuals with a diverging palette so 0 is a neutral colour, and maybe reds are +ve and blues are -ve. You might also look at the tmap package for mapping spatial data.
    – Spacedman
    Nov 11 at 17:29
















Aye the gray() function needs the first parameter to be [0,1] bounded. You can use scales::rescale() to scale your [-40,20] to [0,1]
– hrbrmstr
Nov 11 at 17:13




Aye the gray() function needs the first parameter to be [0,1] bounded. You can use scales::rescale() to scale your [-40,20] to [0,1]
– hrbrmstr
Nov 11 at 17:13












Unless you are printing in black and white, its usually better to plot residuals with a diverging palette so 0 is a neutral colour, and maybe reds are +ve and blues are -ve. You might also look at the tmap package for mapping spatial data.
– Spacedman
Nov 11 at 17:29




Unless you are printing in black and white, its usually better to plot residuals with a diverging palette so 0 is a neutral colour, and maybe reds are +ve and blues are -ve. You might also look at the tmap package for mapping spatial data.
– Spacedman
Nov 11 at 17:29












1 Answer
1






active

oldest

votes

















up vote
0
down vote













col = grey() produces HEX codes for grey scale based on the values placed inside. Yes, these numbers should be between 0:1. If you are trying to get an individual grey HEX code for each unique residual, you can do this by using the length and unique function. Below is an example where we have different grey scale for each group is our data frame:



Group <- c(1,1,1,1,2,2,2,2,3,3,3,3)
Year <- c(2006,2007,2008,2009,2006,2007,2008,2009,2006,2007,2008,2009)
Qtr.1 <- as.numeric(c(15,13,22,13,12,16,13,23,11,15,17,14))
DF <- data.frame(Group,Year,Qtr.1)

#to get HEX for each unique input
c <- grey(1:length(unique(DF$Group))/length(unique(DF$Group)))

c
> [1] "#555555" "#AAAAAA" "#FFFFFF"

#Without Grey
plot(DF$Year,DF$Qtr.1)

#With Grey
plot(DF$Year,DF$Qtr.1, col = c[Group])


Note that I didn't make a choice about the grey HEX codes that were produced, and group 3's HEX code is so faint that is hard to spot. Also, we needed to index on Group at the end of the col argument. As others have mentioned, there are likely better ways to go about this than grey scale and the base plot function. Looking towards other packages is likely a better option for customized visualization (i.e. ggplot or what others have noted).



Check this nice summary for more info on col






share|improve this answer























    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',
    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%2f53250841%2fplot-shapefile-variable-error-invalid-gray-level-must-be-in-0-1-r%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    col = grey() produces HEX codes for grey scale based on the values placed inside. Yes, these numbers should be between 0:1. If you are trying to get an individual grey HEX code for each unique residual, you can do this by using the length and unique function. Below is an example where we have different grey scale for each group is our data frame:



    Group <- c(1,1,1,1,2,2,2,2,3,3,3,3)
    Year <- c(2006,2007,2008,2009,2006,2007,2008,2009,2006,2007,2008,2009)
    Qtr.1 <- as.numeric(c(15,13,22,13,12,16,13,23,11,15,17,14))
    DF <- data.frame(Group,Year,Qtr.1)

    #to get HEX for each unique input
    c <- grey(1:length(unique(DF$Group))/length(unique(DF$Group)))

    c
    > [1] "#555555" "#AAAAAA" "#FFFFFF"

    #Without Grey
    plot(DF$Year,DF$Qtr.1)

    #With Grey
    plot(DF$Year,DF$Qtr.1, col = c[Group])


    Note that I didn't make a choice about the grey HEX codes that were produced, and group 3's HEX code is so faint that is hard to spot. Also, we needed to index on Group at the end of the col argument. As others have mentioned, there are likely better ways to go about this than grey scale and the base plot function. Looking towards other packages is likely a better option for customized visualization (i.e. ggplot or what others have noted).



    Check this nice summary for more info on col






    share|improve this answer



























      up vote
      0
      down vote













      col = grey() produces HEX codes for grey scale based on the values placed inside. Yes, these numbers should be between 0:1. If you are trying to get an individual grey HEX code for each unique residual, you can do this by using the length and unique function. Below is an example where we have different grey scale for each group is our data frame:



      Group <- c(1,1,1,1,2,2,2,2,3,3,3,3)
      Year <- c(2006,2007,2008,2009,2006,2007,2008,2009,2006,2007,2008,2009)
      Qtr.1 <- as.numeric(c(15,13,22,13,12,16,13,23,11,15,17,14))
      DF <- data.frame(Group,Year,Qtr.1)

      #to get HEX for each unique input
      c <- grey(1:length(unique(DF$Group))/length(unique(DF$Group)))

      c
      > [1] "#555555" "#AAAAAA" "#FFFFFF"

      #Without Grey
      plot(DF$Year,DF$Qtr.1)

      #With Grey
      plot(DF$Year,DF$Qtr.1, col = c[Group])


      Note that I didn't make a choice about the grey HEX codes that were produced, and group 3's HEX code is so faint that is hard to spot. Also, we needed to index on Group at the end of the col argument. As others have mentioned, there are likely better ways to go about this than grey scale and the base plot function. Looking towards other packages is likely a better option for customized visualization (i.e. ggplot or what others have noted).



      Check this nice summary for more info on col






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        col = grey() produces HEX codes for grey scale based on the values placed inside. Yes, these numbers should be between 0:1. If you are trying to get an individual grey HEX code for each unique residual, you can do this by using the length and unique function. Below is an example where we have different grey scale for each group is our data frame:



        Group <- c(1,1,1,1,2,2,2,2,3,3,3,3)
        Year <- c(2006,2007,2008,2009,2006,2007,2008,2009,2006,2007,2008,2009)
        Qtr.1 <- as.numeric(c(15,13,22,13,12,16,13,23,11,15,17,14))
        DF <- data.frame(Group,Year,Qtr.1)

        #to get HEX for each unique input
        c <- grey(1:length(unique(DF$Group))/length(unique(DF$Group)))

        c
        > [1] "#555555" "#AAAAAA" "#FFFFFF"

        #Without Grey
        plot(DF$Year,DF$Qtr.1)

        #With Grey
        plot(DF$Year,DF$Qtr.1, col = c[Group])


        Note that I didn't make a choice about the grey HEX codes that were produced, and group 3's HEX code is so faint that is hard to spot. Also, we needed to index on Group at the end of the col argument. As others have mentioned, there are likely better ways to go about this than grey scale and the base plot function. Looking towards other packages is likely a better option for customized visualization (i.e. ggplot or what others have noted).



        Check this nice summary for more info on col






        share|improve this answer














        col = grey() produces HEX codes for grey scale based on the values placed inside. Yes, these numbers should be between 0:1. If you are trying to get an individual grey HEX code for each unique residual, you can do this by using the length and unique function. Below is an example where we have different grey scale for each group is our data frame:



        Group <- c(1,1,1,1,2,2,2,2,3,3,3,3)
        Year <- c(2006,2007,2008,2009,2006,2007,2008,2009,2006,2007,2008,2009)
        Qtr.1 <- as.numeric(c(15,13,22,13,12,16,13,23,11,15,17,14))
        DF <- data.frame(Group,Year,Qtr.1)

        #to get HEX for each unique input
        c <- grey(1:length(unique(DF$Group))/length(unique(DF$Group)))

        c
        > [1] "#555555" "#AAAAAA" "#FFFFFF"

        #Without Grey
        plot(DF$Year,DF$Qtr.1)

        #With Grey
        plot(DF$Year,DF$Qtr.1, col = c[Group])


        Note that I didn't make a choice about the grey HEX codes that were produced, and group 3's HEX code is so faint that is hard to spot. Also, we needed to index on Group at the end of the col argument. As others have mentioned, there are likely better ways to go about this than grey scale and the base plot function. Looking towards other packages is likely a better option for customized visualization (i.e. ggplot or what others have noted).



        Check this nice summary for more info on col







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 17:47

























        answered Nov 11 at 17:04









        Peter_Evan

        2138




        2138






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53250841%2fplot-shapefile-variable-error-invalid-gray-level-must-be-in-0-1-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

            Bressuire

            Vorschmack

            Quarantine