在R传单中使用菱形,三角形和星形 [英] using diamond, triangle and star shapes in R leaflet

查看:85
本文介绍了在R传单中使用菱形,三角形和星形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同类型的观察结果,我想使用不同的形状和颜色将其显示在传单上. R中的传单中可以使用菱形,三角形,星形和其他形状吗?

I have different types of observations and I want to display them on leaflet using different shapes and colors. Is it possible to use diamond, triangle, star and other shapes in leaflet in R?

我提供了虚拟数据并创建了具有不同颜色的圆圈标记.

I have providing dummy data and created circle markers with differnt colors.

library(leaflet)

 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))

 pal <- colorFactor(c("red","blue","purple"), domain = c(1,2,3))


 leaflet(data_all) %>% addTiles() %>%
   addCircleMarkers(~Longitude, ~Latitude,popup=~paste0("Group=  ",data_all$Group),
     radius = 10,
    color = ~pal(Group),
    stroke = FALSE, fillOpacity = 1
     )

推荐答案

这是使用基R绘图符号的解决方案.基本上,您将创建具有所需形状的一系列临时png文件,然后使用它们根据组来表示数据.

Here's a solution using base R plotting symbols. Basically, you create a series of temporary png files with the desired shape which you then use to represent your data according to the group.

library(leaflet)

# this is modified from 
# https://github.com/rstudio/leaflet/blob/master/inst/examples/icons.R#L24
pchIcons = function(pch = 1, width = 30, height = 30, bg = "transparent", col = "black", ...) {
  n = length(pch)
  files = character(n)
  # create a sequence of png images
  for (i in seq_len(n)) {
    f = tempfile(fileext = '.png')
    png(f, width = width, height = height, bg = bg)
    par(mar = c(0, 0, 0, 0))
    plot.new()
    points(.5, .5, pch = pch[i], col = col[i], cex = min(width, height) / 8, ...)
    dev.off()
    files[i] = f
  }
  files
}

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))

shapes = c(5, 6, 8) # base R plotting symbols (http://www.statmethods.net/advgraphs/parameters.html)
iconFiles = pchIcons(shapes, 40, 40, col = c("red", "blue", "purple"), lwd = 4)


leaflet(data_all) %>% addTiles() %>%
  addMarkers(
    data = data_all,
    icon = ~ icons(
      iconUrl = iconFiles[Group],
      popupAnchorX = 20, popupAnchorY = 0
    ),
    popup=~paste0("Group=  ",data_all$Group)
  )

很明显,您可以在addMarkers中使用其他png文件. 此解决方案基于 https:中一个相当隐蔽的示例: //github.com/rstudio/leaflet/blob/master/inst/examples/icons.R#L24

Obviously, you could use other png files in addMarkers. This solution is based on a rather hidden example from https://github.com/rstudio/leaflet/blob/master/inst/examples/icons.R#L24

这篇关于在R传单中使用菱形,三角形和星形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆