Created
June 25, 2015 06:43
-
-
Save mdsumner/a3d2277bae7cd8082302 to your computer and use it in GitHub Desktop.
Heatmap on a globe Lesson #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A recreation of http://gis.stackexchange.com/questions/152094/heatmap-on-globe-distortion-problems | |
```{r} | |
library(raster) | |
library(geosphere) | |
library(MASS) | |
r <- raster(extent(-180, 180, -90, 90), nrow = 256, ncol = 256) | |
nn <- 300 | |
#x <- randomCoordinates(nn) | |
x <- cbind(runif(nn, -180, 180), runif(nn, -90, 90)) | |
kde <- raster(kde2d(x[,1], x[,2], h = c(bandwidth.nrd(x[,1]), bandwidth.nrd(x[,2]))/5, n = nrow(r), lims = c(xmin(r), xmax(r), ymin(r), ymax(r)))) | |
library(rgl) | |
library(rglgris) ## devtools::install_github("mdsumner/rglgris") | |
## build a quadmesh from raster | |
b <- bgl(r) | |
## sphere | |
b$vb[1:3,] <- t(llh2xyz(t(b$vb[1:3,]))) | |
scl <- function(x) (x - min(x))/diff(range(x)) | |
shade3d(b, col = rep(terrain.colors(25)[scl(values(kde)) * 24 + 1], each = 4)) | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment