如何在我的函数中插入更多颜色?我设法只插入了 4 个(红色、蓝色、绿色和薰衣草色)。但如您所见,集群 1 和集群 5 具有相同的颜色(蓝色)。但是,我不希望它们相同。这是一个小例子,但我有超过 10 个集群的数据库,所以我想调整我的颜色分配功能。
可执行代码如下。
非常感谢!
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")