【问题标题】:How to insert more colors in my map that was generated in R如何在 R 中生成的地图中插入更多颜色
【发布时间】:2021-10-21 22:21:46
【问题描述】:

如何在函数中插入更多颜色?我设法只插入了 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")

【问题讨论】:

  • 如果您使用 googleway 包,这 4 种颜色是唯一可用的颜色,请参阅此处的 colour 参数 rdrr.io/cran/googleway/man/add_markers.html
  • 我无法测试,因为显然您需要 API 密钥。
  • 感谢您的回答。除了 googleway 之外,还有其他软件包可以像我那样显示属性的位置吗?那样的话,我可以试试看能不能把这种颜色区分放在另一个包里。
  • 是否需要使用谷歌地图?
  • 遗憾的是!我已经从 Openstreetmap 获得了它,现在它从 Google 地图中消失了。

标签: r google-maps googleway


【解决方案1】:

R 的 googleway 包只有“红色”、“蓝色”、“绿色”或“薰衣草”作为颜色选项,如第 32 行所示:https://github.com/cran/googleway/blob/master/R/google_map_layer_marker.R#L32

如果可能,使用其他标记颜色将需要编写 javascript,如以下 SO 问题之一的答案所示:

Javascript, Change google map marker color

How can I change the color of a Google Maps marker?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-23
    • 2013-06-10
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多