[R-sig-Geo] creating a data range for color coding and using it in color_manual_scale

Roger Bivand Roger@B|v@nd @end|ng |rom nhh@no
Mon Mar 16 17:55:33 CET 2020


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: Roger.Bivand using nhh.no
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en



More information about the R-sig-Geo mailing list