Skip to content

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

3 messages · PAMELLA KAGELIZA KILAVI, Roger Bivand

#
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.
#
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
#
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 <Roger.Bivand at nhh.no> wrote: