creating a data range for color coding and using it in color_manual_scale

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

creating a data range for color coding and using it in color_manual_scale

PAMELLA KAGELIZA KILAVI
Dear All.

Kindly assist me with the proper code for the following. I have converted
the dose to factor and grouped it into 5 levels. I need to assign a color
code to each of the levels and use is for spatial mapping. Thank you

ggplot(Mrima, mapping=aes(x=lon,y=lat))+
geom_point(data=Mrima, aes(colour=factor(dose),
cut(dose,br=c(0,60,120,360,600,5144)))) +
scale_color_manual(my_col, values = c("#D01556#", "#DF785B","EFDC60",
"#B5D374","#7CCA89"),breaks=waiver(), aesthetics =c("colour","fill"))

This is the error I am getting

Error: Insufficient values in manual scale. 1806 needed but only 5 provided.

--

The University of Nairobi is ISO 9001:2015 certified.
Website:
http://www.uonbi.ac.ke/ <http://www.uonbi.ac.ke/>
Facebook:
https://www.facebook.com/uonbi.ac.ke <https://www.facebook.com/uonbi.ac.ke>

Twitter: @uonbi¬† https://twitter.com/uonb <https://twitter.com/uonb>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: creating a data range for color coding and using it in color_manual_scale

Roger Bivand
Administrator
On Mon, 16 Mar 2020, PAMELLA KAGELIZA KILAVI wrote:

> Dear All.
>
> Kindly assist me with the proper code for the following. I have converted
> the dose to factor and grouped it into 5 levels. I need to assign a color
> code to each of the levels and use is for spatial mapping. Thank you
>
> ggplot(Mrima, mapping=aes(x=lon,y=lat))+
> geom_point(data=Mrima, aes(colour=factor(dose),
> cut(dose,br=c(0,60,120,360,600,5144)))) +
> scale_color_manual(my_col, values = c("#D01556#", "#DF785B","EFDC60",
> "#B5D374","#7CCA89"),breaks=waiver(), aesthetics =c("colour","fill"))
>
> This is the error I am getting
>
> Error: Insufficient values in manual scale. 1806 needed but only 5 provided.
>

Please either make the Mrima object available for download (provide a URL,
do not attach), or use a built-in data set to reproduce the problem. I
think that the arguments to geom_point() are either not as expected, or
should be named, or both. I further think that Mrima should be as sf
object, and if you then use tmap (or tmap or mapview for webmaps), life
may become much easier. See perhaps:
https://geocompr.robinlovelace.net/adv-map.html,
https://www.r-spatial.org/r/2018/10/25/ggplot2-sf.html,
https://r-spatial.github.io/sf/articles/sf5.html

Using the North Carolina SIDS data set, I get:

> library(sf)
Linking to GEOS 3.8.0, GDAL 3.1.0dev-e52a02d452, PROJ 7.0.0
> nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
> nc_sp <- st_transform(nc, 2264)
> nc_pts <- st_centroid(nc_sp)
Warning message:
In st_centroid.sf(nc_sp) :
   st_centroid assumes attributes are constant over geometries of x
> nc_df <- nc_pts
> pts <- st_coordinates(nc_df)
> st_geometry(nc_df) <- NULL
> nc_df$x <- pts[,1]
> nc_df$y <- pts[,2]
> library(ggplot2)
> ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
aes(colour=factor(BIR74), cut(BIR74, br=c(0, 1000, 2000, 3000, 4000,
25000)))) + scale_colour_manual(my_col, values=c("#D01556#",
"#DF785B","EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
=c("colour","fill"))
Error in ggproto(NULL, super, call = match.call(), aesthetics =
aesthetics,  :
   object 'my_col' not found
> ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
aes(colour=factor(BIR74), cut(BIR74, br=c(0, 1000, 2000, 3000, 4000,
25000)))) + scale_colour_manual(values=c("#D01556#",
"#DF785B","EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
=c("colour","fill"))
Error: Insufficient values in manual scale. 100 needed but only 5
provided.
> ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
aes(colour=factor(cut(BIR74, br=c(0, 1000, 2000, 3000, 4000, 25000))))) +
scale_colour_manual(values=c("#D01556#",
"#DF785B","EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
=c("colour","fill"))
Error: Malformed colour string `#D01556#`. Must contain either 6 or 8 hex
values
> ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
aes(colour=factor(cut(BIR74, br=c(0, 1000, 2000, 3000, 4000, 25000))))) +
scale_colour_manual(values=c("#D01556",
"#DF785B","EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
=c("colour","fill"))
Error: Unknown colour name: EFDC60
> ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
aes(colour=factor(cut(BIR74, br=c(0, 1000, 2000, 3000, 4000, 25000))))) +
scale_colour_manual(values=c("#D01556",
"#DF785B","#EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
=c("colour","fill"))
> nc_df$dose_f <- factor(cut(nc_df$BIR74, br=c(0, 1000, 2000, 3000, 4000,
25000)))
> ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
aes(colour=dose_f)) + scale_colour_manual(values=c("#D01556",
"#DF785B","#EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
=c("colour","fill"))

As an alternative, since we already had an sf object:

> library(tmap)
> map <- tm_shape(nc_pts) + tm_symbols(size=0.5, col="BIR74",
style="fixed", breaks=c(0, 1000, 2000, 3000, 4000, 25000),
palette=c("#D01556", "#DF785B","#EFDC60","#B5D374","#7CCA89")) +
tm_legend(outside=TRUE)
> map

Extra bonus:

> tmap_mode("view")
> map

> Mrima_sf <- st_as_sf(Mrima, coords=c("lon", "lat"), crs=4326)

will create an sf object defined in decimal degrees (possibly your lon and
lat if they are decimal degrees).

Hope this helps,

Roger

--
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: [hidden email]
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway
Reply | Threaded
Open this post in threaded view
|

Re: creating a data range for color coding and using it in color_manual_scale

PAMELLA KAGELIZA KILAVI
Thank you. I actually have converted Mrima into sf to be able to use the
lon and lat after filtering it from the main file fetched by readxl of
different files downloaded from Safecast bGeiger nano Counter.

the continuous data on continuous scale worked out perfectly with
scale_color_viridis. However,  However, we are interested in highlighting
areas that are hot spots around the area. We, therefore, need the data such
that we classed kind of post map (as used in Surfer program) such that the
geo-referenced dose rates are grouped into coded discrete classes.

Let me try out. I will get back to you with the progress

Once again thank you for the feedback

On Mon, Mar 16, 2020 at 7:55 PM Roger Bivand <[hidden email]> wrote:

> On Mon, 16 Mar 2020, PAMELLA KAGELIZA KILAVI wrote:
>
> > Dear All.
> >
> > Kindly assist me with the proper code for the following. I have converted
> > the dose to factor and grouped it into 5 levels. I need to assign a color
> > code to each of the levels and use is for spatial mapping. Thank you
> >
> > ggplot(Mrima, mapping=aes(x=lon,y=lat))+
> > geom_point(data=Mrima, aes(colour=factor(dose),
> > cut(dose,br=c(0,60,120,360,600,5144)))) +
> > scale_color_manual(my_col, values = c("#D01556#", "#DF785B","EFDC60",
> > "#B5D374","#7CCA89"),breaks=waiver(), aesthetics =c("colour","fill"))
> >
> > This is the error I am getting
> >
> > Error: Insufficient values in manual scale. 1806 needed but only 5
> provided.
> >
>
> Please either make the Mrima object available for download (provide a URL,
> do not attach), or use a built-in data set to reproduce the problem. I
> think that the arguments to geom_point() are either not as expected, or
> should be named, or both. I further think that Mrima should be as sf
> object, and if you then use tmap (or tmap or mapview for webmaps), life
> may become much easier. See perhaps:
> https://geocompr.robinlovelace.net/adv-map.html,
> https://www.r-spatial.org/r/2018/10/25/ggplot2-sf.html,
> https://r-spatial.github.io/sf/articles/sf5.html
>
> Using the North Carolina SIDS data set, I get:
>
> > library(sf)
> Linking to GEOS 3.8.0, GDAL 3.1.0dev-e52a02d452, PROJ 7.0.0
> > nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
> > nc_sp <- st_transform(nc, 2264)
> > nc_pts <- st_centroid(nc_sp)
> Warning message:
> In st_centroid.sf(nc_sp) :
>    st_centroid assumes attributes are constant over geometries of x
> > nc_df <- nc_pts
> > pts <- st_coordinates(nc_df)
> > st_geometry(nc_df) <- NULL
> > nc_df$x <- pts[,1]
> > nc_df$y <- pts[,2]
> > library(ggplot2)
> > ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
> aes(colour=factor(BIR74), cut(BIR74, br=c(0, 1000, 2000, 3000, 4000,
> 25000)))) + scale_colour_manual(my_col, values=c("#D01556#",
> "#DF785B","EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
> =c("colour","fill"))
> Error in ggproto(NULL, super, call = match.call(), aesthetics =
> aesthetics,  :
>    object 'my_col' not found
> > ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
> aes(colour=factor(BIR74), cut(BIR74, br=c(0, 1000, 2000, 3000, 4000,
> 25000)))) + scale_colour_manual(values=c("#D01556#",
> "#DF785B","EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
> =c("colour","fill"))
> Error: Insufficient values in manual scale. 100 needed but only 5
> provided.
> > ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
> aes(colour=factor(cut(BIR74, br=c(0, 1000, 2000, 3000, 4000, 25000))))) +
> scale_colour_manual(values=c("#D01556#",
> "#DF785B","EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
> =c("colour","fill"))
> Error: Malformed colour string `#D01556#`. Must contain either 6 or 8 hex
> values
> > ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
> aes(colour=factor(cut(BIR74, br=c(0, 1000, 2000, 3000, 4000, 25000))))) +
> scale_colour_manual(values=c("#D01556",
> "#DF785B","EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
> =c("colour","fill"))
> Error: Unknown colour name: EFDC60
> > ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
> aes(colour=factor(cut(BIR74, br=c(0, 1000, 2000, 3000, 4000, 25000))))) +
> scale_colour_manual(values=c("#D01556",
> "#DF785B","#EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
> =c("colour","fill"))
> > nc_df$dose_f <- factor(cut(nc_df$BIR74, br=c(0, 1000, 2000, 3000, 4000,
> 25000)))
> > ggplot(nc_df, mapping=aes(x=x, y=y)) + geom_point(data=nc_df,
> aes(colour=dose_f)) + scale_colour_manual(values=c("#D01556",
> "#DF785B","#EFDC60","#B5D374","#7CCA89"),breaks=waiver(), aesthetics
> =c("colour","fill"))
>
> As an alternative, since we already had an sf object:
>
> > library(tmap)
> > map <- tm_shape(nc_pts) + tm_symbols(size=0.5, col="BIR74",
> style="fixed", breaks=c(0, 1000, 2000, 3000, 4000, 25000),
> palette=c("#D01556", "#DF785B","#EFDC60","#B5D374","#7CCA89")) +
> tm_legend(outside=TRUE)
> > map
>
> Extra bonus:
>
> > tmap_mode("view")
> > map
>
> > Mrima_sf <- st_as_sf(Mrima, coords=c("lon", "lat"), crs=4326)
>
> will create an sf object defined in decimal degrees (possibly your lon and
> lat if they are decimal degrees).
>
> Hope this helps,
>
> Roger
>
> --
> Roger Bivand
> Department of Economics, Norwegian School of Economics,
> Helleveien 30, N-5045 Bergen, Norway.
> voice: +47 55 95 93 55; e-mail: [hidden email]
> https://orcid.org/0000-0003-2392-6140
> https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
>

--

The University of Nairobi is ISO 9001:2015 certified.
Website:
http://www.uonbi.ac.ke/ <http://www.uonbi.ac.ke/>
Facebook:
https://www.facebook.com/uonbi.ac.ke <https://www.facebook.com/uonbi.ac.ke>

Twitter: @uonbi¬† https://twitter.com/uonb <https://twitter.com/uonb>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo