r-rmarkdown中的传单热图

r-rmarkdown中的传单热图,r,r-markdown,R,R Markdown,问题 你能在r标记文档中绘制热图吗 示例工作代码 当直接从R运行时,此代码将生成带有热图的传单图 library(rCharts) ## data dat <- data.frame(Longitude = c(-122.3809, -122.3269, -122.3342, -122.2984, -122.3044, -122.2754), Latitude = c(47.66796,47.63436,47.57665,47.71930,47.60616,4

问题

你能在
r标记
文档中绘制热图吗

示例工作代码

当直接从R运行时,此代码将生成带有热图的传单图

library(rCharts)

## data
dat <- data.frame(Longitude = c(-122.3809, -122.3269, -122.3342, -122.2984, -122.3044, -122.2754),
              Latitude = c(47.66796,47.63436,47.57665,47.71930,47.60616,47.55392),
              intensity = c(10,20,30,40,50,300))

## create JSON for heatmap
## - could also use jsonlite::toJSON
j <- paste0("[",dat[,"Latitude"], ",", dat[,"Longitude"], ",", dat[,"intensity"], "]", collapse=",")
j <- paste0("[",j,"]")

## create leaflet map
leaf_map <- Leaflet$new()
leaf_map$setView(c(47.5982623,-122.3415519), zoom=10)
leaf_map$tileLayer(provider="Acetate.terrain")

## add heatmap plugin
leaf_map$addAssets(jshead = c("http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"))

## heatmap layer
leaf_map$setTemplate(afterScript = sprintf("
    <script> 
    var addressPoints = %s
    var heat = L.heatLayer(addressPoints).addTo(map)           
    </script>
    ", j
))

## output map
leaf_map

有可能让“热量”也显示在地图上吗?

多亏了这个问题的答案,我才明白这一点

首先,我为
部分创建了一个
html
文档,将
src
添加到热图插件中,并将其保存在与我的
.Rmd
文件相同的目录中,文件名为
include\js\u heatmap.html

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<style>
    #map { width: 800px; height: 600px; }
    body { font: 16px/1.4 "Helvetica Neue", Arial, sans-serif; }
    .ghbtns { position: relative; top: 4px; margin-left: 5px; }
    a { color: #0077ff; }
</style> 

<script src = "http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"></script>
更新

方法将在
.Rmd
文件旁边创建一个目录,其中包含各种
.html
.js
.css
文件。创建后,标准的
rCharts
代码将不再需要
html代码:

## create leaflet map
leaf_map <- Leaflet$new()
leaf_map$setView(c(47.5982623,-122.3415519), zoom=10)
leaf_map$tileLayer(provider="Acetate.terrain")

## add heatmap plugin
leaf_map$addAssets(jshead = c("http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"))

## heatmap layer
leaf_map$setTemplate(afterScript = sprintf("
    <script> 
    var addressPoints = %s
    var heat = L.heatLayer(addressPoints).addTo(map)           
    </script>
    ", j
))

## output map
leaf_map 
---
title: heatmapRMarkdown
output:
  html_document:
    self_contained: false
    keep_md: true
    includes:
      in_header: "include_js_heatmap.html"
---

```{r}
dat <- data.frame(Longitude = c(-122.3809, -122.3269, -122.3342, -122.2984, -122.3044, -122.2754),
                  Latitude = c(47.66796,47.63436,47.57665,47.71930,47.60616,47.55392),
                  intensity = c(100,20,30,400,50,3))

j <- paste0("[",dat[,"Latitude"], ",", dat[,"Longitude"], ",", dat[,"intensity"], "]", collapse=",")
j <- paste0("[",j,"]")

```

<div id="map"></div>

<script src="http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"></script>

<script>
var map = L.map('map').setView([47.5982623,-122.3415519], 12);

var tiles = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);

addressPoints = `r j` ;

var heat = L.heatLayer(addressPoints).addTo(map);
</script>
## create leaflet map
leaf_map <- Leaflet$new()
leaf_map$setView(c(47.5982623,-122.3415519), zoom=10)
leaf_map$tileLayer(provider="Acetate.terrain")

## add heatmap plugin
leaf_map$addAssets(jshead = c("http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"))

## heatmap layer
leaf_map$setTemplate(afterScript = sprintf("
    <script> 
    var addressPoints = %s
    var heat = L.heatLayer(addressPoints).addTo(map)           
    </script>
    ", j
))

## output map
leaf_map 
```{r baseMap, results='asis', echo=FALSE, comment=NA, cache=FALSE}
library(rCharts)
dat <- data.frame(Longitude = c(-122.3809, -122.3269, -122.3342, -122.2984, -122.3044, -122.2754),
                  Latitude = c(47.66796,47.63436,47.57665,47.71930,47.60616,47.55392),
                  intensity = c(100,20,30,400,50,3))

j <- paste0("[",dat[,"Latitude"], ",", dat[,"Longitude"], ",", dat[,"intensity"], "]", collapse=",")
j <- paste0("[",j,"]")

```

<!-- add heatmap html-->
<div id="heatmap"></div>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<style>
  #heatmap { width: 800px; height: 600px; }
  body { font: 16px/1.4 "Helvetica Neue", Arial, sans-serif; }
  .ghbtns { position: relative; top: 4px; margin-left: 5px; }
  a { color: #0077ff; }
</style> 
<script src="http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"></script>

<script>
  var heatmap = L.map('heatmap').setView([47.5982623,-122.3415519], 12);
  var tiles = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
    }).addTo(heatmap);
  addressPoints = `r j` ;
  var heat = L.heatLayer(addressPoints).addTo(heatmap);
</script>