knitr::opts_chunk$set( comment = "#>", error = FALSE, tidy = FALSE, fig.width=5, fig.height=5, fig.align = "center" )
library(mapster)
The package contains some custom functions to facilitate quick plotting 2d globe projections (lines, etc).
The information that is printed must be suplied in a dataframe. Also the attributes (e.g. cex
) must be supplied as columns in the dataframe.
The information can be supplied as 3d cartesian, spherical coordinates (lattitude and longitude) or projected user coordinates. As an example, we will use the endpoints of the standard cartesian axes.
x <- data.frame(x=c(1, 0, 0, -1, 0, 0), y=c(0, 1, 0, 0, -1, 0), z=c(0, 0, 1, 0, 0, -1)) rownames(x) <- c("X+", "Y+", "Z+", "X-", "Y-", "Z-") x
First, a projection has to be set. This can be done using map_proj
. Several presets are available.
map_proj(preset="ortho") # use presets and set before projecting
Calling map_data
will look which type of coordinates are available and add the missing ones basd on the given projection method.
s <- map_data(x, preset="ortho") s
Additional information for each point can be supplied, that is used by the functions.
s$label <- rownames(s) s$col <- rep(c("darkgreen", "red"), each=3) s$cex <- 1 s
Now the data is ready for visualization.
par(mar=rep(1,4)) map_setup() map_grid() map_points(s) map_labels(s)
Choose type of projection and modify some arguments by hand.
par(mar=rep(1,4)) map_proj("mollweide") # set projection type s <- map_data(s) # recalculate data, can usually be omitted map_setup() map_grid() map_points(s, cex=2:3, col=scales::alpha(s$col, .3)) map_labels(s, label=1:6)
Create random data
set.seed(0) x <- random_data(20) x$cex <- .7
Mollweide projection with some lines between points.
par(mar=rep(1,4)) map_proj("mollweide") # use presets and set before projecting x <- map_data(x) map_setup() map_grid() map_points(x, col="black", cex=1) map_labels(x) map_lines(x[4, ], x[2, ], col="red") map_lines(x[c(1,7,8,9,16), ], x[14, ], col="blue")
Two more projection types (see \code{mapproject::mapproj} for more types).
par(mar=rep(1,4), mfrow=c(1,2)) map_proj("gall") # use presets and set before projecting x <- map_data(x) map_setup() map_grid() map_points(x, col=rainbow(20), cex=2) map_labels(x) map_proj("mercator") # use presets and set before projecting x <- map_data(x) map_setup() map_grid() map_points(x, col="blue", cex=1:20/10) map_text(x, cex=1:20/10, pos=1)
A simple form of projected convex hulls is implemented. Note that non-projected convex hulls can also be overlaid using, for example, the convexhull
package from github.com/markheckmann/convexhull to draw non-projected convex hulls on top of the globe. In the following example two sets of points are enclosed by a hull.
library(convexhull) map_proj("mollweide") # use presets and set before projecting x <- map_data(x) map_setup() map_grid() map_points(x) map_labels(x) ii <- c(4,10,2, 20, 13) map_convex_hull(x[ii, ], col="#FF000030") map_text(x[4, ], label="projected", col="blue", cex=1.5, pos=2) # standard non-projected convex hulls pp <- c_proj(x) ii <- c(1,5,7,9,14,19) convex_hull(pp[ii, ], col="#0000FF30", smooth=2, shape = 2) map_text(x[7, ], label="non-projected", col="blue", cex=1.5, pos=3)
map_setup() map_grid() map_points(x, "phi >= 0", col="darkgreen") map_labels(x, "phi >= 0")
Add centroid using convenience wrappers.
map_setup() map_grid() ss <- .(phi >= 0) map_points(x, ss) map_centroid_point(x, ss, col="red") map_centroid_star(x, ss, col="red") map_centroid_label(x, "centroid", ss, pos=4)
Add a centroid point by hand using centroid
to calculate its position.
map_setup() map_grid() c <- centroid(x, ss) # calc centroid coordinates map_points(c, cex=2, pch=17, col="blue") s <- subset2(x, ss) # select a subset of points map_lines(s, c, col="blue")
To prepare the coordinates, two functions are used internally.
add_sphere_coords
takes the x
, y
, and z
column and adds spherical coordinates phi
, theta
, r
.
x <- data.frame(x = runif(4), y = runif(4), z = runif(4)) x <- round(x, 1) s <- add_sphere_coords(x) s
add_proj_data
adds the projections in user coordinates xp
and yp
based on the spherical coords.
s <- add_proj_data(s) s
The function map_data
combines the two steps. This function is also called inside most functions to preprocess the data.
s <- map_data(x) s
Convenience functions for extracting coordinates from the data.
c_cart(s) c_sphere(s) c_proj(s)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.