From 73721f3d6c513e260f1bb4439a3b82c2d6c52657 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 6 Sep 2017 18:26:39 +1000 Subject: [PATCH] Made code style consistent. --- R/geom-quiver.r | 48 ++++++++++++++++--------------- R/stat-quiver.r | 27 ++++++++++------- tests/testthat/test-geom_quiver.R | 30 ++++++++++--------- 3 files changed, 58 insertions(+), 47 deletions(-) diff --git a/R/geom-quiver.r b/R/geom-quiver.r index 74cf89b..ef9e87b 100644 --- a/R/geom-quiver.r +++ b/R/geom-quiver.r @@ -32,14 +32,14 @@ #' #' @export geom_quiver <- function(mapping = NULL, data = NULL, - stat = "quiver", position = "identity", - center = FALSE, - rescale = FALSE, - vecsize = NULL, - na.rm = FALSE, - show.legend = NA, - inherit.aes = TRUE, - ...) { + stat = "quiver", position = "identity", + center = FALSE, + rescale = FALSE, + vecsize = NULL, + na.rm = FALSE, + show.legend = NA, + inherit.aes = TRUE, + ...) { ggplot2::layer( data = data, mapping = mapping, @@ -61,19 +61,21 @@ geom_quiver <- function(mapping = NULL, data = NULL, #' @rdname geom_quiver #' #' @export -GeomQuiver <- ggproto("GeomQuiver", ggplot2::GeomSegment, - draw_panel = function(data, panel_params, coord, arrow = NULL, lineend = "butt", na.rm = FALSE) { - trans <- CoordCartesian$transform(data, panel_params) %>% - mutate(arrowsize = sqrt((x-xend)^2 + (y-yend)^2)*0.5) - grid::segmentsGrob( - trans$x, trans$y, trans$xend, trans$yend, - default.units = "native", - gp = grid::gpar( - col = alpha(trans$colour, trans$alpha), - lwd = trans$size * .pt, - lty = trans$linetype, - lineend = lineend), - arrow = grid::arrow(length = unit(trans$arrowsize, "npc")) - ) - } +GeomQuiver <- ggproto( + "GeomQuiver", ggplot2::GeomSegment, + draw_panel = function(data, panel_params, coord, arrow = NULL, lineend = "butt", na.rm = FALSE) { + trans <- CoordCartesian$transform(data, panel_params) %>% + mutate(arrowsize = sqrt((x - xend) ^ 2 + (y - yend) ^ 2) * 0.5) + grid::segmentsGrob( + trans$x, trans$y, trans$xend, trans$yend, + default.units = "native", + gp = grid::gpar( + col = alpha(trans$colour, trans$alpha), + lwd = trans$size * .pt, + lty = trans$linetype, + lineend = lineend + ), + arrow = grid::arrow(length = unit(trans$arrowsize, "npc")) + ) + } ) diff --git a/R/stat-quiver.r b/R/stat-quiver.r index 033e11a..38f017e 100644 --- a/R/stat-quiver.r +++ b/R/stat-quiver.r @@ -17,8 +17,7 @@ stat_quiver <- function(mapping = NULL, data = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, - ...) -{ + ...) { ggplot2::layer( data = data, mapping = mapping, @@ -48,22 +47,28 @@ StatQuiver <- ggplot2::ggproto( required_aes = c("u", "v"), compute_panel = function(self, data, scales, center=FALSE, rescale=FALSE, vecsize=NULL, na.rm=FALSE) { - if(rescale){ + if (rescale) { data <- data %>% mutate(u = as.numeric(scale(u)), v = as.numeric(scale(v))) } gridpoints <- c(abs(diff(sort(unique(data$x)))), abs(diff(sort(unique(data$y))))) gridsize <- min(gridpoints, na.rm = TRUE) - if(is.null(vecsize)){ + if (is.null(vecsize)) { # Detect if x and y form a grid - vecsize <- if(length(unique(round(gridpoints, 2))) > 2) 0 else 1 + vecsize <- if (length(unique(round(gridpoints, 2))) > 2) 0 else 1 } - center <- if(center) 0.5 else 0 + center <- if (center) 0.5 else 0 data %>% - filter(u!=0 | v!=0) %>% - mutate(veclength = sqrt(u^2 + v^2), - vectorsize = if(vecsize==0){1}else{gridsize/max(veclength, na.rm=TRUE) * vecsize}, - xend = x + (1-center)*u*vectorsize, yend = y + (1-center)*v*vectorsize, - x = x - center*u*vectorsize, y = y - center*v*vectorsize) + filter(u != 0 | v != 0) %>% + mutate( + veclength = sqrt(u ^ 2 + v ^ 2), + vectorsize = if (vecsize == 0) { + 1 + } else { + gridsize / max(veclength, na.rm = TRUE) * vecsize + }, + xend = x + (1 - center) * u * vectorsize, yend = y + (1 - center) * v * vectorsize, + x = x - center * u * vectorsize, y = y - center * v * vectorsize + ) } ) diff --git a/tests/testthat/test-geom_quiver.R b/tests/testthat/test-geom_quiver.R index beda6c5..5826a3a 100644 --- a/tests/testthat/test-geom_quiver.R +++ b/tests/testthat/test-geom_quiver.R @@ -2,29 +2,33 @@ library(dplyr) context("geom_quiver") test_that("Simple trig quiver plot", { - plotdata <- expand.grid(x=seq(0,pi,pi/12), y=seq(0,pi,pi/12)) %>% - mutate(u = cos(x), - v = sin(y)) + plotdata <- expand.grid(x = seq(0, pi, pi / 12), y = seq(0, pi, pi / 12)) %>% + mutate( + u = cos(x), + v = sin(y) + ) plotdata %>% - ggplot(aes(x=x,y=y,u=u,v=v)) + + ggplot(aes(x = x, y = y, u = u, v = v)) + geom_quiver() plotdata %>% - ggplot(aes(x=x,y=y,u=u,v=v)) + - geom_quiver(rescale=TRUE) + ggplot(aes(x = x, y = y, u = u, v = v)) + + geom_quiver(rescale = TRUE) plotdata %>% - ggplot(aes(x=x,y=y,u=u,v=v)) + - geom_quiver(center=TRUE) + ggplot(aes(x = x, y = y, u = u, v = v)) + + geom_quiver(center = TRUE) plotdata %>% - ggplot(aes(x=x,y=y,u=u,v=v)) + + ggplot(aes(x = x, y = y, u = u, v = v)) + geom_quiver(vecsize = 0) - data.frame(x=rnorm(10), y=rnorm(10)) %>% - mutate(u = cos(x), - v = sin(y)) %>% - ggplot(aes(x=x,y=y,u=u,v=v)) + + data.frame(x = rnorm(10), y = rnorm(10)) %>% + mutate( + u = cos(x), + v = sin(y) + ) %>% + ggplot(aes(x = x, y = y, u = u, v = v)) + geom_quiver() })