R传单,来自非可变数据的标记颜色

R leaflet, markers color from non variable data

你好,我是 R 的初学者。我想创建一个带有标记的地图,以显示企业客户的位置。在我的数据中,我有 23 列包含有关客户的不同信息。

我有一列 "type.de.compte",其中包含 2 个非数值变量:配镜师和眼科医生。

我苦苦挣扎的是我的标记有 2 种不同的颜色,如果客户是配镜师则为红色,如果他是眼科医生则为蓝色。

可能是一个愚蠢的问题,但我正在努力解决这个问题..

这是我目前拥有的:

icons1 <- awesomeIcons(
  icon = 'ion-ios-body',
  iconColor = 'black',
  library = 'ion',
  markerColor = "blue")

 leaflet(data = origAddress) %>% addTiles() %>%
   addAwesomeMarkers(~long,~lat,
                icon=icons1,
                popup=paste(origAddress$Nom.du.compte,
                            "Ville :"origAddress$Ville,
                            "Téléphone:"origAddress$Téléphone.principal))

提前致谢。

您可以预先创建以下命名向量:

customer_colors = c("optician"='red',
                  "opthalmologist"='blue')

# optionally, for any exceptions:
colors[is.na(colors)]="white" 

然后你可以使用下面的代码,假设你的数据帧被称为df:

colors = customer_colors[df$type.de.compte]

icons1 <- awesomeIcons(
  icon = 'ion-ios-body',
  iconColor = 'black',
  library = 'ion',
  markerColor = unname(colors))

希望对您有所帮助!