Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ importFrom(dplyr,across)
importFrom(dplyr,all_of)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_cols)
importFrom(dplyr,bind_rows)
importFrom(dplyr,everything)
importFrom(dplyr,group_by)
importFrom(dplyr,last_col)
Expand Down
22 changes: 9 additions & 13 deletions R/ggfacet.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#' p <- ggfacet(NIR_sub, x_cols, y_cols, scales = "fixed")
#' p_(p)
#' }
#' @importFrom dplyr arrange .data reframe
ggfacet <- function(
data, mapping = NULL,
columnsX = 1:ncol(data),
Expand Down Expand Up @@ -91,19 +92,14 @@ ggfacet <- function(
columnLabelsY <- columnLabelsY[!is_factor_y]
}

tall_data <- ddply(
expand.grid(.x_col = columnsX, .y_col = columnsY),
c(".x_col", ".y_col"),
function(row) {
x_var <- row$.x_col[1]
y_var <- row$.y_col[1]

ret <- data
ret[[".x_val"]] <- data[[x_var]]
ret[[".y_val"]] <- data[[y_var]]
ret
}
)
tall_data <- expand.grid(.x_col = columnsX, .y_col = columnsY) %>%
reframe(
.by = c(".x_col", ".y_col"),
data,
.x_val = data[[.data$.x_col]],
.y_val = data[[.data$.y_col]]
) %>%
arrange(.x_col,, .y_col)

if (is.null(mapping)) {
mapping <- aes()
Expand Down
13 changes: 6 additions & 7 deletions R/ggnetworkmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ if (getRversion() >= "2.15.1") {
#' @param ... other arguments supplied to geom_text for the node labels. Arguments pertaining to the title or other items can be achieved through \pkg{ggplot2} methods.
#' @author Amos Elberg. Original by Moritz Marbach, Francois Briatte
#' @details This is a function for plotting graphs generated by \code{network} or \code{igraph} in a more flexible and elegant manner than permitted by ggnet. The function does not need to be the first plot in the ggplot chain, so the graph can be plotted on top of a map or other chart. Segments can be straight lines, or plotted as great circles. Note that the great circles feature can produce odd results with arrows and with vertices beyond the plot edges; this is a \pkg{ggplot2} limitation and cannot yet be fixed. Nodes can have two color schemes, which are then plotted as the center and ring around the node. The color schemes are selected by adding scale_fill_ or scale_color_ just like any other \pkg{ggplot2} plot. If there are no rings, scale_color sets the color of the nodes. If there are rings, scale_color sets the color of the rings, and scale_fill sets the color of the centers. Note that additional arguments in the ... are passed to geom_text for plotting labels.
#' @importFrom dplyr bind_rows
#' @importFrom utils installed.packages
#' @examples
#' # small function to display plots only if it's interactive
Expand Down Expand Up @@ -325,11 +326,9 @@ ggnetworkmap <- function(
pts <- 25 # number of intermediate points for drawing great circles
i <- 0 # used to keep track of groups when getting intermediate points for great circles

edges <- ddply(
.data = edges,
.variables = c("lat1", "lat2", "lon1", "lon2"),
.parallel = FALSE,
.fun = function(x) {
edges <- edges %>%
split(edges[, c("lat1", "lat2", "lon1", "lon2")]) %>%
lapply(function(x) {
p1Mat <- x[, c("lon1", "lat1")]
colnames(p1Mat) <- NULL
p2Mat <- x[, c("lon2", "lat2")]
Expand Down Expand Up @@ -370,8 +369,8 @@ ggnetworkmap <- function(
return(ret)
}
}
}
)
}) %>%
bind_rows()

edge_aes$x <- substitute(lon)
edge_aes$y <- substitute(lat)
Expand Down
17 changes: 14 additions & 3 deletions tests/testthat/test-ggfacet.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@

skip_if_not_installed("chemometrics")
test_that("simple test with iris data", {
p <- ggfacet(iris, columnsX = 1:2, columnsY = 3:4)
expect_s3_class(p, "ggplot")
expect_equal(dim(p$data), c(4L * nrow(iris), ncol(iris) + 4L))

data(NIR, package = "chemometrics")
NIR_sub <- data.frame(NIR$yGlcEtOH, NIR$xNIR[, 1:3])
expect_equal(
dim(ggfacet(mtcars, columnsX = 1:2, columnsY = 3:5)$data),
c(6L * nrow(mtcars), ncol(mtcars) + 4L)
)
})

test_that("warnings", {
expect_warning(
Expand All @@ -16,6 +22,11 @@ test_that("warnings", {
})

test_that("generally works", {
skip_if_not_installed("chemometrics")

data(NIR, package = "chemometrics")
NIR_sub <- data.frame(NIR$yGlcEtOH, NIR$xNIR[, 1:3])

# factor variables
vdiffr::expect_doppelganger(
"factor",
Expand Down