R Leaflet - 使用带有 colorNumeric() 调色板的日期或字符图例标签 [英] R Leaflet - use date or character legend labels with colorNumeric() palette

查看:27
本文介绍了R Leaflet - 使用带有 colorNumeric() 调色板的日期或字符图例标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 lat/lon 和时间戳的数据集.我希望标记的颜色用连续的调色板显示时间.我正在使用 colorNumeric() 和之前使用 julian(x, "2015-01-01") 创建的朱利安日期.

data = structure(list(timestamp = structure(c(1434056453, 1434148216, 1434153635, 1434245436, 1434358840,1434364288, 1434369611, 1434461435, 1434466830, 1434558725), 类 = c("POSIXct", "POSIXt"), tzone = ""),lon = c(-119.8777, -119.9614, -119.8769, -119.8775, -120.2283,-120.2285, -119.8429, -120.0954, -120.3957, -120.4421),纬度 = c(34.4041,34.376, 34.4061, 34.4021, 34.4696,34.4697, 34.1909, 34.4328, 34.4554, 34.4456),ID = as.factor(c("Z11","Z05","Z01","Z04","Z11","Z04","Z01","Z05","Z05","Z11"))),.Names = c("timestamp", "lon", "lat", "ID"),row.names = c(1:10),类=data.frame")data$julian = as.numeric(julian(data$timestamp, origin = "2015-01-01"))朋友=颜色数字(调色板=彩虹(7),域=数据$朱利安)m = 传单(数据)m %>% addTiles() %>%addCircles(~lon, ~lat, color = ~pal(julian)) %>%addLegend("bottomright", pal = pal, values = ~julian, title = "Time", opacity = 1)

图例将标签显示为数字朱利安日期:我希望它们以2015-01-01"或类似格式显示为正确"日期.
为此,我使用

as.Date(x, origin=as.Date("2015-01-01"))

但插入时它不起作用进入 addLegend()

addLegend(pal = pal, values = ~julian,labFormat = labelFormat(transform = ~as.Date(julian, origin=as.Date("2015-01-01"))))

有没有办法修改图例标签以显示日期和/或字符?

解决方案

来自

I have a dataset with lat/lon and a timestamp. I want the color of the markers to show time with a continous palette. I am using colorNumeric() with julian dates earlier created using julian(x, "2015-01-01").

data = structure(list(timestamp = structure(c(1434056453, 1434148216, 1434153635, 1434245436, 1434358840, 
                                          1434364288, 1434369611, 1434461435, 1434466830, 1434558725), class = c("POSIXct", "POSIXt"), tzone = ""), 
                  lon = c(-119.8777, -119.9614, -119.8769, -119.8775, -120.2283, 
                          -120.2285, -119.8429, -120.0954, -120.3957, -120.4421), 
                  lat = c(34.4041,34.376, 34.4061, 34.4021, 34.4696, 
                          34.4697, 34.1909, 34.4328, 34.4554, 34.4456), 
                  ID = as.factor(c("Z11","Z05","Z01", "Z04", "Z11", "Z04","Z01","Z05","Z05","Z11"))), 
             .Names = c("timestamp", "lon", "lat", "ID"),
             row.names = c(1:10), 
             class = "data.frame")
data$julian = as.numeric(julian(data$timestamp, origin = "2015-01-01"))

pal = colorNumeric(  palette = rainbow(7), domain = data$julian) 

m = leaflet(data)
m %>% addTiles() %>% 
      addCircles(~lon, ~lat, color = ~pal(julian)) %>% 
      addLegend("bottomright", pal = pal, values = ~julian, title = "Time", opacity = 1)

The legend shows the labels as numeric, julian dates: I want them to show as "proper" dates in a format like "2015-01-01" or similar.
To do this, I use

as.Date(x, origin=as.Date("2015-01-01"))

but it does not work when I insert it into addLegend() with

addLegend(pal = pal, values = ~julian, 
           labFormat = labelFormat(transform = ~as.Date(julian, origin=as.Date("2015-01-01"))))

Is there a way to modify legend labels so that they show dates and/or characters?

解决方案

From the leaflet page on legends:

You can also conveniently customize the label appearance by passing labFormat=labelFormat(). labelFormat() has parameters that customize the separator between ranges, the number of digits to render, and prefix/suffix for each label. If your label formatting needs extend beyond what labelFormat() can provide, you can also use a custom function as the labFormat argument; see the Details section in ?addLegend for a description.

Therefore, we can modify the source code for the labelFormat function to include a custom function to convert dates

myLabelFormat = function(
  prefix = '', suffix = '', between = ' – ', digits = 3, big.mark = ',',
  transform = identity, dates = FALSE   ## new 'dates' argument
) {

  formatNum = function(x) {
    format(
      round(transform(x), digits), trim = TRUE, scientific = FALSE,
      big.mark = big.mark
    )
  }
  ## added 'formatDate' function
  formatDate = function(x) {
    d = as.Date(x, origin="1970-01-01")
  }

  function(type, ...) {
    switch(
      type,
      numeric = (function(cuts) {
        if(dates){   
          ## will format numbers into dates if dates == TRUE
          paste0(formatDate(cuts))
        }else{
          paste0(prefix, formatNum(cuts), suffix)
        }
      })(...),
      bin = (function(cuts) {
        n = length(cuts)
        paste0(prefix, formatNum(cuts[-n]), between, formatNum(cuts[-1]), suffix)
      })(...),
      quantile = (function(cuts, p) {
        n = length(cuts)
        p = paste0(round(p * 100), '%')
        cuts = paste0(formatNum(cuts[-n]), between, formatNum(cuts[-1]))
        # mouse over the legend labels to see the values (quantiles)
        paste0(
          '<span title="', cuts, '">', prefix, p[-n], between, p[-1], suffix,
          '</span>'
        )
      })(...),
      factor = (function(cuts) {
        paste0(prefix, as.character(transform(cuts)), suffix)
      })(...)
    )
  }
}


Which, as @Nice points out can be shortened to

myLabelFormat = function(...,dates=FALSE){ 
  if(dates){ 
    function(type = "numeric", cuts){ 
      as.Date(cuts, origin="1970-01-01")
    } 
  }else{
    labelFormat(...)
  }
}


With this new function we can call it as normal

data = structure(list(timestamp = structure(c(1434056453, 1434148216, 1434153635, 1434245436, 1434358840, 
                                              1434364288, 1434369611, 1434461435, 1434466830, 1434558725), class = c("POSIXct", "POSIXt"), tzone = ""), 
                      lon = c(-119.8777, -119.9614, -119.8769, -119.8775, -120.2283, 
                              -120.2285, -119.8429, -120.0954, -120.3957, -120.4421), 
                      lat = c(34.4041,34.376, 34.4061, 34.4021, 34.4696, 
                              34.4697, 34.1909, 34.4328, 34.4554, 34.4456), 
                      ID = as.factor(c("Z11","Z05","Z01", "Z04", "Z11", "Z04","Z01","Z05","Z05","Z11"))), 
                 .Names = c("timestamp", "lon", "lat", "ID"),
                 row.names = c(1:10), 
                 class = "data.frame")

data$julian <- as.numeric(as.Date(data$timestamp))


library(leaflet)

pal = colorNumeric(  palette = rainbow(7), domain = data$julian) 

m = leaflet(data)
m %>% addTiles() %>% 
  addCircles(~lon, ~lat, color = ~pal(julian)) %>% 
  addLegend("bottomright", pal = pal, values = ~julian, 
             title = "Time", opacity = 1, 
             labFormat = myLabelFormat(dates=TRUE))

这篇关于R Leaflet - 使用带有 colorNumeric() 调色板的日期或字符图例标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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