2016-12-29 11 views
1

私はさまざまなタイプの観察をしています。さまざまな形や色を使ってリーフレットに表示したいと思います。ダイヤモンド、三角形、星、その他の形状をRのリーフレットに使用することは可能ですか?Rのリーフレットにダイヤモンド、三角形、星形を使用

私はダミーデータを提供し、異なる色の円マーカーを作成しました。

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 
    ) 

enter image description here

答えて

1

ここで記号をプロットベースRを使用したソリューションです。基本的には、目的の形状の一連の一時的なPNGファイルを作成し、それをグループに従ってデータを表現するために使用します。

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