传单 R 中具有不同形状和颜色的自定义标记
Custom markers with different shapes and colors in leaflet R
传单中有一些创建自定义标记的示例,。但是,有很多数据包含多个因素,最好用不同的形状和颜色进行可视化。
这里是虚拟数据,如何将不同形状和颜色的标记点起来?
lat1= 36+runif(n=5,min=-1,max=1)
lon1 =-115+runif(n=5,min=-1,max=1)
lat2= 35+runif(n=5,min=-0.5,max=0.5)
lon2 =-110+runif(n=5,min=-0.5,max=0.5)
lat3= 34+runif(n=5,min=-0.5,max=0.5)
lon3 =-112+runif(n=5,min=-0.5,max=0.5)
data_all=rbind(data.frame(Longitude=lon1,Latitude=lat1,Group=1),
data.frame(Longitude=lon2,Latitude=lat2,Group=2),
data.frame(Longitude=lon3,Latitude=lat3,Group=3))
data_all$color <- rep(c("red", "green", "gray"), 5)
请注意:根据 help page,"grey" 不是受支持的颜色,而 "gray" 是......所以我更改了它。
data_all$color <- rep(c("red", "green", "gray"), 5)
我用过这个page for help ; you can try the following method, using awesomeIcons
, with icons and colors defined in the dataset (using the Bootstrap Glyphicons library)
# add icon label column
data_all <- data_all %>%
mutate(icon = case_when(
Group == 1 ~ "home",
Group == 2 ~ "cog",
Group == 3 ~ "camera"))
# create awesome icons
my_icons <- awesomeIcons(icon = data_all$icon,
markerColor = data_all$color,
library = "glyphicon")
# leaflet using AwesomeMarkers
data_all %>%
leaflet() %>%
addTiles() %>%
addAwesomeMarkers(lng = ~ Longitude, lat = ~ Latitude, icon = ~ my_icons[Group])
编辑
如果你想要特定的图标,我最好的选择是创建你自己的图标列表,并将其与你的数据相关联(afaik,你不能直接向 addMarkers
参数添加颜色)。
以下是有效的,但它可能不会在庞大的数据集上表现良好(请参阅代码中的不同注释)。
# add "group_color" column as a factor variable : this will be associated to the icons' list
data_all2 <- data_all %>%
mutate(Group = case_when(
Group == 1 ~ "triangle",
Group == 2 ~ "circle",
Group == 3 ~ "square"),
group_color = as.factor(paste(Group, color, sep = "_")))
# # Make a list of icons. We'll index into it based on name.
# /!\ order of icons MUST BE THE SAME as the order of the factor "group_color"
my_icons2 <- iconList(
circle_gray <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/black-circle-icon-23.png",
iconWidth = 18, iconHeight = 18),
circle_green <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/green-circle-icon-28.png",
iconWidth = 18, iconHeight = 18),
circle_red <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/red-circle-icon-1.png",
iconWidth = 18, iconHeight = 18),
square_gray <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/black-square-frame-23.png",
iconWidth = 18, iconHeight = 18),
square_green <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/green-square-1.png",
iconWidth = 18, iconHeight = 18),
square_red <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/red-square-png-14.png",
iconWidth = 18, iconHeight = 18),
triangle_gray <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/triangle-png-28.png",
iconWidth = 18, iconHeight = 18),
triangle_green <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/green-normal-triangle-png-8.png",
iconWidth = 18, iconHeight = 18),
triangle_red <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/red-triangle-png-20.png",
iconWidth = 18, iconHeight = 18)
)
# leaflet using addMArkers
data_all2 %>%
leaflet() %>%
addTiles() %>%
# for some reason, we have to use the 'as.numeric' version of the factor, I don't really know why
addMarkers(lng = ~ Longitude, lat = ~ Latitude, icon = ~ my_icons2[as.numeric(group_color)],
popup = ~ paste0("Group = ", Group, "<br>Color = ", color))
结果:
EDIT2
一个不太麻烦的解决方案可以将第二种解决方案与 addCircleMarkers
结合起来:这些不是彩色图标,而是不同颜色的图标。
my_icons2 <- iconList(
circle <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/black-circle-icon-23.png",
iconWidth = 18, iconHeight = 18),
square <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/black-square-frame-23.png",
iconWidth = 18, iconHeight = 18),
triangle <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/triangle-png-28.png",
iconWidth = 18, iconHeight = 18)
)
# using original data_all dataframe
# leaflet using addCirleMarkers and addMArkers
data_all %>%
leaflet() %>%
addTiles() %>%
# specific circle color according to the 'color' column
addCircleMarkers(lng = ~ Longitude, lat = ~ Latitude, color = ~ color, fillColor = ~ color, opacity = 0.8, radius = 15, fillOpacity = 0.8) %>%
# specific icon shape according to the 'Group' column
addMarkers(lng = ~ Longitude, lat = ~ Latitude, icon = ~ my_icons2[Group],
popup = ~ paste0("Group = ", Group, "<br>Color = ", color))
你应该得到这样的东西:不是彩色图标,而是足够容易理解的东西。
希望对您有所帮助。
传单中有一些创建自定义标记的示例,
这里是虚拟数据,如何将不同形状和颜色的标记点起来?
lat1= 36+runif(n=5,min=-1,max=1)
lon1 =-115+runif(n=5,min=-1,max=1)
lat2= 35+runif(n=5,min=-0.5,max=0.5)
lon2 =-110+runif(n=5,min=-0.5,max=0.5)
lat3= 34+runif(n=5,min=-0.5,max=0.5)
lon3 =-112+runif(n=5,min=-0.5,max=0.5)
data_all=rbind(data.frame(Longitude=lon1,Latitude=lat1,Group=1),
data.frame(Longitude=lon2,Latitude=lat2,Group=2),
data.frame(Longitude=lon3,Latitude=lat3,Group=3))
data_all$color <- rep(c("red", "green", "gray"), 5)
请注意:根据 help page,"grey" 不是受支持的颜色,而 "gray" 是......所以我更改了它。
data_all$color <- rep(c("red", "green", "gray"), 5)
我用过这个page for help ; you can try the following method, using awesomeIcons
, with icons and colors defined in the dataset (using the Bootstrap Glyphicons library)
# add icon label column
data_all <- data_all %>%
mutate(icon = case_when(
Group == 1 ~ "home",
Group == 2 ~ "cog",
Group == 3 ~ "camera"))
# create awesome icons
my_icons <- awesomeIcons(icon = data_all$icon,
markerColor = data_all$color,
library = "glyphicon")
# leaflet using AwesomeMarkers
data_all %>%
leaflet() %>%
addTiles() %>%
addAwesomeMarkers(lng = ~ Longitude, lat = ~ Latitude, icon = ~ my_icons[Group])
编辑
如果你想要特定的图标,我最好的选择是创建你自己的图标列表,并将其与你的数据相关联(afaik,你不能直接向 addMarkers
参数添加颜色)。
以下是有效的,但它可能不会在庞大的数据集上表现良好(请参阅代码中的不同注释)。
# add "group_color" column as a factor variable : this will be associated to the icons' list
data_all2 <- data_all %>%
mutate(Group = case_when(
Group == 1 ~ "triangle",
Group == 2 ~ "circle",
Group == 3 ~ "square"),
group_color = as.factor(paste(Group, color, sep = "_")))
# # Make a list of icons. We'll index into it based on name.
# /!\ order of icons MUST BE THE SAME as the order of the factor "group_color"
my_icons2 <- iconList(
circle_gray <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/black-circle-icon-23.png",
iconWidth = 18, iconHeight = 18),
circle_green <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/green-circle-icon-28.png",
iconWidth = 18, iconHeight = 18),
circle_red <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/red-circle-icon-1.png",
iconWidth = 18, iconHeight = 18),
square_gray <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/black-square-frame-23.png",
iconWidth = 18, iconHeight = 18),
square_green <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/green-square-1.png",
iconWidth = 18, iconHeight = 18),
square_red <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/red-square-png-14.png",
iconWidth = 18, iconHeight = 18),
triangle_gray <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/triangle-png-28.png",
iconWidth = 18, iconHeight = 18),
triangle_green <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/green-normal-triangle-png-8.png",
iconWidth = 18, iconHeight = 18),
triangle_red <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/red-triangle-png-20.png",
iconWidth = 18, iconHeight = 18)
)
# leaflet using addMArkers
data_all2 %>%
leaflet() %>%
addTiles() %>%
# for some reason, we have to use the 'as.numeric' version of the factor, I don't really know why
addMarkers(lng = ~ Longitude, lat = ~ Latitude, icon = ~ my_icons2[as.numeric(group_color)],
popup = ~ paste0("Group = ", Group, "<br>Color = ", color))
结果:
EDIT2
一个不太麻烦的解决方案可以将第二种解决方案与 addCircleMarkers
结合起来:这些不是彩色图标,而是不同颜色的图标。
my_icons2 <- iconList(
circle <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/black-circle-icon-23.png",
iconWidth = 18, iconHeight = 18),
square <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/black-square-frame-23.png",
iconWidth = 18, iconHeight = 18),
triangle <- makeIcon(iconUrl = "https://www.freeiconspng.com/uploads/triangle-png-28.png",
iconWidth = 18, iconHeight = 18)
)
# using original data_all dataframe
# leaflet using addCirleMarkers and addMArkers
data_all %>%
leaflet() %>%
addTiles() %>%
# specific circle color according to the 'color' column
addCircleMarkers(lng = ~ Longitude, lat = ~ Latitude, color = ~ color, fillColor = ~ color, opacity = 0.8, radius = 15, fillOpacity = 0.8) %>%
# specific icon shape according to the 'Group' column
addMarkers(lng = ~ Longitude, lat = ~ Latitude, icon = ~ my_icons2[Group],
popup = ~ paste0("Group = ", Group, "<br>Color = ", color))
你应该得到这样的东西:不是彩色图标,而是足够容易理解的东西。
希望对您有所帮助。