如何在TMap HTML多边形弹出窗口或固定宽度中换行长文本字符串?TMap可以识别换行符吗? [英] How can I wrap long text strings in tmap HTML polygon popups or fixed widths? Can tmap recognize line breaks?

查看:6
本文介绍了如何在TMap HTML多边形弹出窗口或固定宽度中换行长文本字符串?TMap可以识别换行符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中使用tmap包(v3.0,我还尝试了v3.2)来创建一个HTML映射。我有一个包含数字和字符文本字段的空间多边形。在我的几个文本字段中,我有一个用分号(;)分隔的项目列表。对于这些多边形中的一些,文本字符串非常长,以至于弹出窗口可能比HTML窗口更宽。我必须缩小地图才能看到这些弹出窗口的内容。

我尝试在这些文本字符串中加入分隔符,以便在弹出窗口的数据字段中换行,但tmap无法识别它们。像 这样的分隔符不会在弹出窗口中创建新行。

下面是尝试将不同类型的分隔符添加到名为long_names的列及其输出的示例:

# install latest version of `tmap` and `tmaptools`
# library(remotes)
# install_github("mtennekes/tmaptools")
# install_github("mtennekes/tmap")

# load libraries
  library(sf)
  library(raster)
  library(spData)
  library(tmaptools)
  library(tmap)

# use spData dataset called nz
# add example row with long list of names
  nz$long_names <- 'long name 1; long name 2; long name 3; long name 4; long name 5'

# try to make multiple lines in string for the popups
# try different codes for breaks
  nz$long_names_n <- gsub("; ", "
", nz$long_names)
  nz$long_names_r <- gsub("; ", "
", nz$long_names)
  nz$long_names_br1 <- gsub("; ", "<br/>", nz$long_names)
  nz$long_names_br2 <- gsub("; ", "<br>", nz$long_names)

# change tmap to view mode
  tmap_mode("view")
 
# plot interactive map
  tm_shape(nz) +
    tm_fill()

输出截图:long string in HTML popup and it doesn't wrap

在我的实际数据集中,我在long_names中列出了不同数量的项目,所以我希望有不同长度的弹出窗口(上下滚动都可以),但宽度相似。

我还尝试使用tmap_leaflet()popupOptions()来调整最大弹出窗口宽度,但它忽略了它,并给出了与以前相同的地图:

# interactive map
  map1 <- tm_shape(nz) +
          tm_fill()

# convert to leaflet object
  lf <- tmap_leaflet(map1)

# assign minimum and maximum width to popup options
  lf <- lf %>% leaflet::popupOptions(minWidth = 50,
                      maxWidth = 50)
# view leaflet version
  lf

输出:

$maxWidth
[1] 50

$minWidth
[1] 50

$maxHeight

$autoPan
[1] TRUE

$keepInView
[1] FALSE

$closeButton
[1] TRUE

$className
[1] ""

截图:same result as before with popup being too wide

我也尝试了popup.format = list(),但我认为它不起作用,因为数据不是数字(请参阅popup.format下的here)。如果我说错了,请纠正我。在同一链接legend.format下,它说我可以放入函数,但我不知道如何使用这些以分号分隔的字符串。

# plot interactive map
  tm_shape(nz) +
  tm_fill(popup.format = list(long_names=list(maxWidth=50)))

错误:

格式C出错(x=c(12500.5611491,4941.5725565,23900.0363831,12071.1446886,:未使用的参数(MaxWidth=50)

我的主要问题:

  1. 有没有办法在弹出窗口中使用tmap换行,或者让tmap识别字符串中的换行符?
  2. 如果没有,如何使用tm_leaflet()leaflet修改tmap弹出选项?我的真实数据集在多边形中有30个字段,有这个问题的6个数据字段--包括每个列表中不同数量的项目--所以如果可能的话,我希望不必单独输入所有30个数据字段(请参见here)。
  3. 或者,是否可以在弹出窗口中包含从左到右的滚动条?

非常感谢您的帮助!

推荐答案

为了回答问题1,我用一个简单的例子做了一个例子,其中我设置了一些坐标,还组成了一些要包括在标签中的示例列。

tibble(
  lon = 149.068778, 
  lat = -35.230741, 
) %>% 
  st_as_sf(
    coords = c(1:2), 
    crs = 4326
  ) %>% 
  mutate(
    city = 'Canberra', 
    country = 'Australia', 
    weather = 'sunny'
    ) %>% 
  {. ->> my_popup}

my_popup 

# Simple feature collection with 1 feature and 3 fields
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 149.0688 ymin: -35.23074 xmax: 149.0688 ymax: -35.23074
# Geodetic CRS:  WGS 84
# # A tibble: 1 x 4
#               geometry city     country   weather
# *          <POINT [°]> <chr>    <chr>     <chr>  
# 1 (149.0688 -35.23074) Canberra Australia sunny  

选项1-告诉tmap在弹出窗口的每一行上显示什么信息

这非常简单,并且不依赖于任何HTML编码或指定换行符。然而,出于同样的原因,它可能相当特定于tmap,并且与您在leaflet中执行此操作的方式或在ggplot中制作标签的方式略有不同。基本上,您使用popup.vars告诉tmap要在标签中显示什么,并提供行标题和该行的数据。

tm_shape(my_popup)+
  tm_dots(size = 1,
          popup.vars = c('City: ' = 'city', 
                         'Country' = 'country', 
                         'Weather: ' = 'weather')
          )

行标题(=左侧)可以是空格,但不能为空;' '可以,但''不可以。

选项2-以HTML格式设置标签的格式

my_popup中创建一个新列,称为label。这是以HTML样式编写的单个文本字符串(这是在leaflet中创建自定义弹出窗口的常见方式)。它可以包括文本字符串、换行符(<br>)、数据列中的值,甚至是具有自定义超链接名的URL链接(我假设还有其他的超链接名称)。

my_popup %>% 
  mutate(
    label = str_c('First line of my label <br>',
                  country, '<br>',
                  'Weather: ', weather, '<br>', 
                  'Link to website: <a href = "https://visitcanberra.com.au/"> Custom link name </a>'
                  )
  ) %>% 
  {. ->> my_popup_1}

my_popup_1

# Simple feature collection with 1 feature and 4 fields
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 149.0688 ymin: -35.23074 xmax: 149.0688 ymax: -35.23074
# Geodetic CRS:  WGS 84
# # A tibble: 1 x 5
#               geometry city     country   weather label                                                                                                                                         
# *          <POINT [°]> <chr>    <chr>     <chr>   <chr>                                                                                                                                         
# 1 (149.0688 -35.23074) Canberra Australia sunny   "First line of my label<br>Australia<br>Weather: sunny<br>Link to website: <a href = "https://visitcanberra.com.au/"> Custom link name </a>"
然后,我们可以将label列指定为tm_dots()中的(唯一)popup.var列。为了让tmap确认该HTML样式,我们必须告诉它不要使用popup.format转义HTML。默认情况下,html.escape = TRUE,说明见?tm_dots

tm_shape(my_popup_1)+
  tm_dots(size = 1,
          popup.vars = 'label',
          popup.format = list(html.escape = FALSE)
  )

请注意,tmap会自动在弹出窗口的左侧以灰色文本的形式包含弹出窗口信息所来自的列的名称-在本例中为label列。这在我们有些复杂且不言自明的标签中是不必要的,但如果弹出窗口中只有一个值,则可能会很方便。

要消除这种情况,我们在popup.vars中将弹出窗口的";行标题";指定为选项1中的,但这次指定为空白(空白似乎可以,空字符串不可以)。

tm_shape(my_popup_1)+
  tm_dots(size = 2,
          popup.vars = c(' ' = 'label'),
          popup.format = list(html.escape = FALSE)
  )

或者,您可以将这两种方法结合起来,以包括HTML(如超级链接)和行标题(包括空格):

my_popup %>% 
  mutate(
    line_1 = 'First line of my label', 
    line_2 = country,
    line_3 = str_c('<a href = "https://visitcanberra.com.au/"> Custom link name </a>'),
    ) %>% 
  {. ->> my_popup_2}

my_popup_2

# Simple feature collection with 1 feature and 6 fields
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 149.0688 ymin: -35.23074 xmax: 149.0688 ymax: -35.23074
# Geodetic CRS:  WGS 84
# # A tibble: 1 x 7
#               geometry city     country   weather line_1                 line_2    line_3                                                              
# *          <POINT [°]> <chr>    <chr>     <chr>   <chr>                  <chr>     <chr>                                                               
# 1 (149.0688 -35.23074) Canberra Australia sunny   First line of my label Australia "<a href = "https://visitcanberra.com.au/"> Custom link name </a>"

tm_shape(my_popup_2)+
  tm_dots(size = 1,
          popup.vars = c('Line 1: ' = 'line_1', 
                         ' ' = 'country', 
                         'Weather: ' = 'weather',
                         'Link to website: ' = 'line_3'), 
          popup.format = list(html.escape = FALSE)
  )

因此,选项1非常简单,但如果您想要更花哨,您可能会希望使用选项2来利用HTML的功能,包括文本颜色和对齐方式(特别是因为默认情况下弹出窗口看起来是右对齐的,这可能不适合)。

例如,文本颜色:

my_popup %>% 
  mutate(
    label = str_c('<p style="color:red">This is my label</p>')
  ) %>% 
  {. ->> my_popup_3}

tm_shape(my_popup_3)+
  tm_dots(size = 1,
          popup.vars = c(' ' = 'label'),
          popup.format = list(html.escape = FALSE)
  )

或调整对齐方式:

my_popup %>% 
  mutate(
    label = str_c('<p align="left">This is my long label<br>More<br>More</p>')
  ) %>% 
  {. ->> my_popup_4}

tm_shape(my_popup_4)+
  tm_dots(size = 1,
          popup.vars = c(' ' = 'label'),
          popup.format = list(html.escape = FALSE)
  )

my_popup %>% 
  mutate(
    label = str_c('<p align="left">This is my long label<br>More<br>More</p>', 
                  '<p align="right">More<br>and<br>More</p>')
  ) %>% 
  {. ->> my_popup_5}

tm_shape(my_popup_5)+
  tm_dots(size = 1,
          popup.vars = c(' ' = 'label'),
          popup.format = list(html.escape = FALSE)
  )

对于问题2,我建议您将层移动到leaflet而不是tmap,并使用上面的HTML调整弹出窗口,而不是修改弹出选项。希望上面的解决方案不需要这样做,但您可以使用如下内容:

tm_leaflet(my_tmap) %>% 
  addMarkers(popup = ~my_label)

问题3-滚动条。我不确定如何/是否可以实现这些,我有一种感觉,当我在弹出窗口中包含一个大图像(使用HTML)时,我已经看到了它们,这必须触发最大弹出窗口大小。希望上面的两个选项足够让你理清头绪,反正你根本不需要滚动条。

这篇关于如何在TMap HTML多边形弹出窗口或固定宽度中换行长文本字符串?TMap可以识别换行符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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