Skip to content
Prev 27927 / 29559 Next

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

On Mon, 16 Mar 2020, PAMELLA KAGELIZA KILAVI wrote:

            
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:
Linking to GEOS 3.8.0, GDAL 3.1.0dev-e52a02d452, PROJ 7.0.0
Warning message:
In st_centroid.sf(nc_sp) :
   st_centroid assumes attributes are constant over geometries of x
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
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.
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
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
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"))
25000)))
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:
style="fixed", breaks=c(0, 1000, 2000, 3000, 4000, 25000),
palette=c("#D01556", "#DF785B","#EFDC60","#B5D374","#7CCA89")) +
tm_legend(outside=TRUE)
Extra bonus:
will create an sf object defined in decimal degrees (possibly your lon and 
lat if they are decimal degrees).

Hope this helps,

Roger