Skip to content

Commit

Permalink
Made code style consistent.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchelloharawild committed Sep 6, 2017
1 parent 68d2f1f commit 73721f3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 47 deletions.
48 changes: 25 additions & 23 deletions R/geom-quiver.r
Expand Up @@ -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,
Expand All @@ -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"))
)
}
)
27 changes: 16 additions & 11 deletions R/stat-quiver.r
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
}
)
30 changes: 17 additions & 13 deletions tests/testthat/test-geom_quiver.R
Expand Up @@ -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()
})

0 comments on commit 73721f3

Please sign in to comment.