在R中使用自定义标签创建传单地图 [英] Creating a Leaflet map with custom labels in R

查看:105
本文介绍了在R中使用自定义标签创建传单地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用R中的世界地图来可视化我的数据,在该世界地图中将在某些点(给定坐标)添加标签.标签应为一些3D矩形,其高度与数据表中的值成比例.我将使用R包"leaflet"(或更好的任何替代方法).世界各地大约有10-15个点,每个位置都有两个值(具体来说,点是主要油田的位置,值例如是大小和储量).我希望每个点都有两个这样的3D矩形,可以说,红色和蓝色彼此靠拢,并具有适当的高度和数字,每个点都用油田名称标记.我找到了带有传单包的解决方案,并以适当的半径将圆添加到地图中.

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

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

但是此解决方案不是很好,因为它不允许可视化第二个值(通过参数radius =〜res.density,其中res.density是我的.csv中盆地第一个值的名称表格).

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

我想复制看起来像这张图片的东西,它是GMT制作的.拥有一个普通的(2D)映射就足够了,但是每个点都需要两个这样的矩形,并带有字段名称和每个矩形的值.

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.

GMT包中的图片

推荐答案

您可以在软件包leaflet.minicharts的帮助下添加条形图,如下所示:

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)

为简单起见,您可以省略addCircleMarkers.

Possibly, you can leave out the addCircleMarkers for simplicity.

这篇关于在R中使用自定义标签创建传单地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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