Creating a Leaflet map with custom labels in R

走远了吗. 提交于 2019-12-07 13:27:08

问题


I would like to visualize my data using a world map in R, where labels are to be added at certain points (given coordinates). The labels should be some 3D-rectangles with heights proportional to the value from data table. I would use the R package "leaflet" (or any alternative, if better). There are about 10-15 points around the world, and there are two values for each location (specifically, points are locations of major oil fields, and values are, for example, size and reserves). I want to have two such 3D rectangles for each point, lets say, red and blue standing near each other, with appropriate heights, and numbers on them, and each point to be labelled with the name of the oil field. I found the solution with leaflet package, adding circles to the map with appropriate radius.

The data and libraries are loaded by the code:

library(leaflet)
basins<-read.csv("somedata.csv")

And somedata.csv has the following structure (four datalines only as minimal working example):

basin,lat,lon,res.density,rel.area
Central Sumatra,1,96,16.7,75
North Sea,58.4,2,20,24
Maracaibo basin,9,-71,74.4,14.3
Los Angeles,33,-118,31.2,32

The circle-labelled map is invoked by the command

m=leaflet(data = basins) %>% addTiles() %>% addCircleMarkers(~lon, ~lat , popup = ~as.character(basin),radius=~res.density*0.4,label=~htmlEscape(basin),labelOptions=labelOptions(noHide=T,textOnly=TRUE,direction="bottom"))

However this solution is not so good, since it does not allow to visualize the second value (via the argument radius=~res.density , where res.density is the name of the first value for the basin in my .csv table).

I would like to reproduce something which looks like this image, which was produced by GMT. It would be sufficient to have a plain (2D) map, however two such rectangles are needed for each point, with name of the field and values for each rectangle.

Image from GMT package


回答1:


You can add the bar chart with help of the package leaflet.minicharts as follows:

library("leaflet")
library("htmltools")
library("leaflet.minicharts")

basins <- read.table(text="basin,lat,lon,res.density,rel.area
Central Sumatra,1,96,16.7,75
North Sea,58.4,2,20,24
Maracaibo basin,9,-71,74.4,14.3
Los Angeles,33,-118,31.2,32", header=T, sep=",")

leaflet(data = basins) %>% 
  addProviderTiles("OpenStreetMap.Mapnik") %>% 
  addLabelOnlyMarkers(lng = ~lon, lat = ~lat, label = ~htmlEscape(basin),
                      labelOptions = labelOptions(noHide = TRUE, textOnly = TRUE, 
                                                  direction = "bottom", offset = c(0,5))) %>%
  addMinicharts(basins$lon, basins$lat, type = "bar", 
                chartdata = basins[, c("res.density", "rel.area")], width = 50, height = 60)

Possibly, you can leave out the addCircleMarkers for simplicity.



来源:https://stackoverflow.com/questions/45538831/creating-a-leaflet-map-with-custom-labels-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!