This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Function to align two ggplot objects when 1 has a legend and one doesn't | |
| ## Copied from https://stackoverflow.com/a/30414008/1710632 on 2018-02-08 | |
| AlignPlots <- function(...) { | |
| LegendWidth <- function(x) x$grobs[[8]]$grobs[[1]]$widths[[4]] | |
| plots.grobs <- lapply(list(...), ggplotGrob) | |
| max.widths <- do.call(unit.pmax, lapply(plots.grobs, "[[", "widths")) | |
| plots.grobs.eq.widths <- lapply(plots.grobs, function(x) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diverge0 <- function(p, ramp) { | |
| # p: a trellis object resulting from rasterVis::levelplot | |
| # ramp: the name of an RColorBrewer palette (as character), a character | |
| # vector of colour names to interpolate, or a colorRampPalette. | |
| require(RColorBrewer) | |
| require(rasterVis) | |
| if(length(ramp)==1 && is.character(ramp) && ramp %in% | |
| row.names(brewer.pal.info)) { | |
| ramp <- suppressWarnings(colorRampPalette(brewer.pal(11, ramp))) | |
| } else if(length(ramp) > 1 && is.character(ramp) && all(ramp %in% colors())) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| stratified <- function(df, group, size, select = NULL, | |
| replace = TRUE, bothSets = FALSE) { | |
| if (is.null(select)) { | |
| df <- df | |
| } else { | |
| if (is.null(names(select))) stop("'select' must be a named list") | |
| if (!all(names(select) %in% names(df))) | |
| stop("Please verify your 'select' argument") | |
| temp <- sapply(names(select), | |
| function(x) df[[x]] %in% select[[x]]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Script from a density to color quantiles area | |
| # Packages --------------------------------------------------------------------- | |
| library(ggplot2) | |
| library(dplyr) | |
| # Data ------------------------------------------------------------------------- | |
| r_data = rnorm(100) # Computes 100 values | |
| r_quant = quantile(r_data, probs = seq(0, 1, 0.1)) # Computes quantiles |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diverge0 <- function(p, ramp) { | |
| # p: a trellis object resulting from rasterVis::levelplot | |
| # ramp: the name of an RColorBrewer palette (as character), a character | |
| # vector of colour names to interpolate, or a colorRampPalette. | |
| require(RColorBrewer) | |
| require(rasterVis) | |
| if(length(ramp)==1 && is.character(ramp) && ramp %in% | |
| row.names(brewer.pal.info)) { | |
| ramp <- suppressWarnings(colorRampPalette(brewer.pal(11, ramp))) | |
| } else if(length(ramp) > 1 && is.character(ramp) && all(ramp %in% colors())) { |
NewerOlder