Browse Source

coord_proj tweaks + update docs

tags/v0.1.1
boB Rudis 8 years ago
parent
commit
7c31151645
  1. 1
      .gitignore
  2. 18
      DESCRIPTION
  3. 56
      R/coord_proj.r
  4. 2
      README.html
  5. 2
      README.md
  6. 47
      man/coord_proj.Rd
  7. 0
      vignettes/.build.timestamp
  8. 24
      vignettes/Introduction to ggalt.Rmd
  9. 7
      vignettes/Introduction_to_ggalt.R
  10. 278
      vignettes/Introduction_to_ggalt.html

1
.gitignore

@ -1,3 +1,4 @@
.Rproj.user
.Rhistory
.RData
inst/doc

18
DESCRIPTION

@ -1,19 +1,22 @@
Package: ggalt
Title: Alternate/Extra 'Geoms', 'Stats' and 'Coords' for 'ggplot2'
Version: 0.1.0.9000
Title: Additional 'Geoms', 'Stats' and 'Coords' for 'ggplot2' 2.0+
Version: 0.1.1.9000
Authors@R: c(person("Bob", "Rudis", email = "bob@rudis.net", role = c("aut", "cre")))
Description: A package containing additional/alternate 'geoms', 'coords' and
'stats' for use with the revamped (late 2015) version of ggplot2.
Description: A package containing additional 'geoms', 'coords' and
'stats' for use with 'ggplot2' 2.0+, including splines, 1d and 2d densities,
univariate average shifted histograms and PROJ.4-based map projections.
Depends:
R (>= 3.0.0),
ggplot2 (>= 1.0.1.9003)
ggplot2 (>= 2.0.0)
License: AGPL + file LICENSE
LazyData: true
Suggests:
testthat,
gridExtra
gridExtra,
knitr,
rmarkdown,
ggthemes
Encoding: UTF-8
Enhances: ggplot2
Imports:
graphics,
grDevices,
@ -27,3 +30,4 @@ Imports:
ash,
MASS
RoxygenNote: 5.0.1
VignetteBuilder: knitr

56
R/coord_proj.r

@ -1,4 +1,5 @@
#' Like \code{coord_map} only better :-)
#' Similar to \code{coord_map} but uses the PROJ.4 library/package for projection
#' transformation
#'
#' The representation of a portion of the earth, which is approximately
#' spherical, onto a flat 2D plane requires a projection. This is what
@ -18,9 +19,46 @@
#' higher always require a datum or an ellipsoid. Set to \code{NA} if no
#' datum should be added to proj (e.g. if you specify an ellipsoid
#' directly).
#' @param xlim manually specific x limits (in degrees of longitude)
#' @param ylim manually specific y limits (in degrees of latitude)
#' @param xlim manually specify x limits (in degrees of longitude)
#' @param ylim manually specify y limits (in degrees of latitude)
#' @note When \code{inverse} is \code{FALSE} \code{coord_proj} makes a fairly
#' large assumption that the coordinates being transformed are within
#' -180:180 (longitude) and -90:90 (latitude). As such, it truncates
#' all longitude & latitude input to fit within these ranges.
#' @export
#' @examples
#' # World in Winkel-Tripel
#' world <- map_data("world")
#' world <- world[world$region != "Antarctica",]
#'
#' gg <- ggplot()
#' gg <- gg + geom_map(data=world, map=world,
#' aes(x=long, y=lat, map_id=region))
#' gg <- gg + coord_proj("+proj=wintri")
#' gg
#'
#' # U.S.A. Albers-style
#' usa <- world[world$region == "USA",]
#' usa <- usa[!(usa$subregion %in% c("Alaska", "Hawaii")),]
#'
#' gg <- ggplot()
#' gg <- gg + geom_map(data=usa, map=usa,
#' aes(x=long, y=lat, map_id=region))
#' gg <- gg + coord_proj(
#' paste0("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96",
#' " +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"))
#' gg
#'
#' # Showcase Greenland (properly)
#' greenland <- world[world$region == "Greenland",]
#'
#' gg <- ggplot()
#' gg <- gg + geom_map(data=greenland, map=greenland,
#' aes(x=long, y=lat, map_id=region))
#' gg <- gg + coord_proj(
#' paste0("+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +k=1 +x_0=0",
#' " +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
#' gg
coord_proj <- function(proj=NULL, inverse = FALSE, degrees = TRUE,
ellps.default="sphere", xlim = NULL, ylim = NULL) {
@ -198,13 +236,17 @@ project4 <- function(coord, x, y) {
df <- data.frame(x=x, y=y)
if (!coord$inverse) {
# map extremes cause issues with projections both with proj4 &
# spTransform. this compensates for them.
df$x <- ifelse(df$x <= -180, -179.999999999, df$x)
df$x <- ifelse(df$x >= 180, 179.999999999, df$x)
df$y <- ifelse(df$y <= -90, -89.999999999, df$y)
df$y <- ifelse(df$y >= 90, 89.999999999, df$y)
df$x <- ifelse(df$x <= -180, -179.99999999999, df$x)
df$x <- ifelse(df$x >= 180, 179.99999999999, df$x)
df$y <- ifelse(df$y <= -90, -89.99999999999, df$y)
df$y <- ifelse(df$y >= 90, 89.99999999999, df$y)
}
suppressWarnings({
res <- proj4::project(list(x=df$x, y=df$y),

2
README.html

@ -101,7 +101,7 @@ library(ggalt)
# current verison
packageVersion(&quot;ggalt&quot;)
#&gt; [1] '0.1.0.9000'
#&gt; [1] '0.1.1.9000'
set.seed(1492)
dat &lt;- data.frame(x=c(1:10, 1:10, 1:10),

2
README.md

@ -50,7 +50,7 @@ library(ggalt)
# current verison
packageVersion("ggalt")
#> [1] '0.1.0.9000'
#> [1] '0.1.1.9000'
set.seed(1492)
dat <- data.frame(x=c(1:10, 1:10, 1:10),

47
man/coord_proj.Rd

@ -2,7 +2,8 @@
% Please edit documentation in R/coord_proj.r
\name{coord_proj}
\alias{coord_proj}
\title{Like \code{coord_map} only better :-)}
\title{Similar to \code{coord_map} but uses the PROJ.4 library/package for projection
transformation}
\usage{
coord_proj(proj = NULL, inverse = FALSE, degrees = TRUE,
ellps.default = "sphere", xlim = NULL, ylim = NULL)
@ -25,9 +26,9 @@ higher always require a datum or an ellipsoid. Set to \code{NA} if no
datum should be added to proj (e.g. if you specify an ellipsoid
directly).}
\item{xlim}{manually specific x limits (in degrees of longitude)}
\item{xlim}{manually specify x limits (in degrees of longitude)}
\item{ylim}{manually specific y limits (in degrees of latitude)}
\item{ylim}{manually specify y limits (in degrees of latitude)}
}
\description{
The representation of a portion of the earth, which is approximately
@ -35,4 +36,44 @@ spherical, onto a flat 2D plane requires a projection. This is what
\code{coord_proj} does, using the \code{proj4::project()} function from
the \code{proj4} package.
}
\note{
When \code{inverse} is \code{FALSE} \code{coord_proj} makes a fairly
large assumption that the coordinates being transformed are within
-180:180 (longitude) and -90:90 (latitude). As such, it truncates
all longitude & latitude input to fit within these ranges.
}
\examples{
# World in Winkel-Tripel
world <- map_data("world")
world <- world[world$region != "Antarctica",]
gg <- ggplot()
gg <- gg + geom_map(data=world, map=world,
aes(x=long, y=lat, map_id=region))
gg <- gg + coord_proj("+proj=wintri")
gg
# U.S.A. Albers-style
usa <- world[world$region == "USA",]
usa <- usa[!(usa$subregion \%in\% c("Alaska", "Hawaii")),]
gg <- ggplot()
gg <- gg + geom_map(data=usa, map=usa,
aes(x=long, y=lat, map_id=region))
gg <- gg + coord_proj(
paste0("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96",
" +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"))
gg
# Showcase Greenland (properly)
greenland <- world[world$region == "Greenland",]
gg <- ggplot()
gg <- gg + geom_map(data=greenland, map=greenland,
aes(x=long, y=lat, map_id=region))
gg <- gg + coord_proj(
paste0("+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +k=1 +x_0=0",
" +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"))
gg
}

0
vignettes/.build.timestamp

24
vignettes/Introduction to ggalt.Rmd

@ -0,0 +1,24 @@
---
title: "Vignette Title"
author: "Bob Rudis"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
The release of `ggplot2` version `2.0` brought with it the ability to easily extend it from within external packages. The `ggalt` package provides additionals "stats", "geoms" and "coords", including:
- `coord_proj` : Similar to `coord_map` but using the PROJ.4 library/package- `geom_xspline` : Connect control points/observations with an X-spline
[geom]
- `stat_xspline` : Connect control points/observations with an X-spline [stat]- `geom_bkde` : Display a smooth density estimate (uses `KernSmooth::bkde`)
[geom]- `stat_bkde` : Display a smooth density estimate (uses `KernSmooth::bkde`)
[stat]- `geom_bkde2d` : Contours from a 2d density estimate. (uses `KernSmooth::bkde2D`)
[geom]- `stat_bkde2d` : Contours from a 2d density estimate. (uses `KernSmooth::bkde2D`)
[stat]- `stat_ash` : Compute and display a univariate averaged shifted histogram (polynomial kernel) (uses `ash::ash1`/`ash::bin1`)
[stat]
## Vignette Info
N

7
vignettes/Introduction_to_ggalt.R

@ -0,0 +1,7 @@
## ---- fig.show='hold'----------------------------------------------------
plot(1:10)
plot(10:1)
## ---- echo=FALSE, results='asis'-----------------------------------------
knitr::kable(head(mtcars, 10))

278
vignettes/Introduction_to_ggalt.html

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save