Reputation: 1111
How do I insert more colors in my function? I managed to insert only 4 (red, blue, green and lavender). But as you can see, cluster 1 and cluster 5 have the same color (blue). However, I didn't want them to be the same. This is a small example, but I have databases with more than 10 clusters, so I would like to adjust my color assignment function.
Executable code below.
Thank you very much!
library(googleway)
library(geosphere)
set_key( "API_KEY" )
swf1<-structure(list(Properties = c(1,2,3,4,5,6), Lat = c(-24.781624,-24.775017,-24.769196,-24.761741,-24.752019,-24.748008),
Lon = c(-49.937369,-49.950576,-49.927608,-49.92762,-49.920608,-49.927707)),
class="data.frame",row.names = c(NA, -6L))
#clusters
d<-as.dist(distm(swf1[,2:1]))
fit.average<-hclust(d,method="average")
clusters<-cutree(fit.average, 5)
swf1$cluster<-clusters
marker_colors<-c("red", "blue", "green", "lavender")
swf1$color <- marker_colors[swf1$cluster%% 4 +1]
swf1
google_map() %>%
add_markers(
data = swf1, lon = "Lon", lat = "Lat", colour="color")
Upvotes: 1
Views: 196
Reputation: 5529
The googleway
package for R has only 'red', 'blue', 'green' or 'lavender' as color options as shown on line 32 here: https://github.com/cran/googleway/blob/master/R/google_map_layer_marker.R#L32
If possible, using additional marker colors will require writing javascript as shown here in the answers from one of the following SO questions:
Javascript, Change google map marker color
How can I change the color of a Google Maps marker?
Upvotes: 1