R Markdown

Moin Niko.

Hier einer erster interaktiver Entwurf (s.u.), den du zum Ausbauen nutzen kannst.

VG Mark

library(ggplot2)
library(plotly)
library(stringr)
library(mapster)


# modify text for hovering
# remove everything before second line break <br> code
#
mod_textinfo <- function(s)
{
  s <- str_split(s, "<br>")
  sapply(s, function(x) paste0(x[-(1:2)], collapse = "<br>"))
}


#### generate data ####

set.seed(0)
n <- 200
x <- random_data(n)
x$cex <- .7
x$group <- sample(paste("group group group", 1:20), n, rep=T)
x$pole <- sample(LETTERS, n, rep=T)


#### grid lines ####

# data for latitudinal lines
lat <- seq(-180, 180, by=30)
lon <- seq(-90, 90, by=3)
d <- expand.grid(theta=lon, 
                 phi=lat, 
                 r=1, 
                 grid="grid")

# data for longitudinal lines
lat <- seq(-180, 180, by=5)
lon <- seq(-90, 90, by=10)
e <- expand.grid(phi=lat, 
                 theta=lon,
                 r=1, 
                 grid="grid")


#### projections ####

map_proj("mollweide")   # use presets and set before projecting
x <- map_data(x)        # project poles
d <- map_data(d)        # project latitudinal lines
e <- map_data(e)        # project longitudinal lines


#### plot ####

gg <- ggplot() + 
  geom_path(aes(xp,yp, fill=grid), col="grey", size=.2, data=d) +
  geom_path(aes(x=xp,yp, fill=grid, group=theta), col="grey", 
            size=.2, data=e) +
  geom_point(aes(xp,yp,color=group, pole=pole), data=x) + 
  theme_void() +
  coord_fixed(ratio = 1)
p <- plotly_build(gg)


#### modify plotly object ####

# see ploty reference: https://plot.ly/r/reference/
# see https://plot.ly/r/lines-on-maps/ for nice 3D maps

# set hover mode to 'text' and modify hover text
p$data <- lapply(p$data, function(x) {
  x$hoverinfo = "text"
  x$text <- mod_textinfo(x$text)
  x
})

# adjust legends and remove hovering 
# for grid lines
p$data[[1]]$showlegend = T
p$data[[1]]$name = "Show gridlines"
p$data[[1]]$legendgroup = "gridlines"
p$data[[2]]$showlegend = F
p$data[[2]]$legendgroup = "gridlines"
p$data[[1]]$hoverinfo = "none"
p$data[[2]]$hoverinfo = "none"