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"’
r regression
add a comment |
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"’
r regression
Aye thegray()
function needs the first parameter to be [0,1] bounded. You can usescales::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 thetmap
package for mapping spatial data.
– Spacedman
Nov 11 at 17:29
add a comment |
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"’
r regression
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
r regression
asked Nov 11 at 16:35
Will.S89
857
857
Aye thegray()
function needs the first parameter to be [0,1] bounded. You can usescales::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 thetmap
package for mapping spatial data.
– Spacedman
Nov 11 at 17:29
add a comment |
Aye thegray()
function needs the first parameter to be [0,1] bounded. You can usescales::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 thetmap
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 11 at 17:47
answered Nov 11 at 17:04
Peter_Evan
2138
2138
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Aye the
gray()
function needs the first parameter to be [0,1] bounded. You can usescales::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