metR (version 0.15.0)

ConvertLongitude: Converts between longitude conventions

Description

Converts longitude from [0, 360) to [-180, 180) and vice versa.

Usage

ConvertLongitude(lon, group = NULL, from = NULL)

Value

If group is missing, a numeric vector the same length of lon. Else, a list with vectors lon and group.

Arguments

lon

numeric vector of longitude

group

optional vector of groups (the same length as longitude) that will be split on the edges (see examples)

from

optionally explicitly say from which convention to convert

Examples

Run this code
library(ggplot2)
library(data.table)
data.table::setDTthreads(1)
data(geopotential)

ggplot(geopotential[date == date[1]], aes(lon, lat, z = gh)) +
    geom_contour(color = "black") +
    geom_contour(aes(x = ConvertLongitude(lon)))

map <- setDT(map_data("world"))
map[, c("lon", "group2") := ConvertLongitude(long, group, from = 180)]

ggplot(map, aes(lon, lat, group = group2)) +
    geom_path()

Run the code above in your browser using DataLab