How to iteratively build and export multiple graphs from 1 data set based on a factor

Here is a small example of how it could be done using for loops with the mtcars dataset for(g in unique(mtcars$gear)) { f <- filter(mtcars, gear == g) p <- ggplot(f, aes(disp, hp)) + geom_point() ggsave(paste0(‘plot_’, g,’.jpg’), p) } In your case it would something like this for(s in unique(WQ_byStation$StationName)){ f <- filter(WQ_byStation, StationName == … Read more

Order Stacked Bar Graph by sum / total of all subgroups

The general (non ggplot-specific) answer is to use reorder() to reset the factor levels in a categorical column, based on some function of the other columns. ## Examine the default factor order levels($fullname) ## Reorder fullname based on the the sum of the other columns$fullname <- reorder($fullname, rowSums([-1])) ## Examine the new factor order … Read more

How to parametrize function calls in dplyr 0.7?

dplyr will have a specialized group_by function group_by_at to deal with multiple grouping variables. It would be much easier to use the new member of the _at family: # using the pre-release 0.6.0 cols <- c(“am”,”gear”) mtcars %>% group_by_at(.vars = cols) %>% summarise(mean_cyl=mean(cyl)) # Source: local data frame [4 x 3] # Groups: am [?] … Read more

Common legend for multiple plots in R

A simple example of what I was talking about: m <- matrix(c(1,2,3,4,5,6,7,7,7),nrow = 3,ncol = 3,byrow = TRUE) layout(mat = m,heights = c(0.4,0.4,0.2)) for (i in 1:6){ par(mar = c(2,2,1,1)) plot(runif(5),runif(5),xlab = “”,ylab = “”) } plot(1, type = “n”, axes=FALSE, xlab=””, ylab=””) plot_colors <- c(“blue”,”black”, “green”, “orange”, “pink”) legend(x = “top”,inset = 0, legend … Read more

Finding 2 & 3 word Phrases Using R TM Package

You can pass in a custom tokenizing function to tm‘s DocumentTermMatrix function, so if you have package tau installed it’s fairly straightforward. library(tm); library(tau); tokenize_ngrams <- function(x, n=3) return(rownames(,method=”string”,n=n))))) texts <- c(“This is the first document.”, “This is the second file.”, “This is the third text.”) corpus <- Corpus(VectorSource(texts)) matrix <- DocumentTermMatrix(corpus,control=list(tokenize=tokenize_ngrams)) Where n in … Read more

Is it possible to stop executing of R code inside shiny (without stopping the shiny process)?

So another answer, outside a loop : use a child process. library(shiny) library(parallel) # # reactive variables # rVal <- reactiveValues() rVal$process <- NULL rVal$msg <- NULL rVal$obs <- NULL counter <- 0 results <- list() dfEmpty <- data.frame(results = numeric(0)) # # Long computation # analyze <- function() { out <- lapply(1:5, function(x) { … Read more

How to add a number of observations per group and use group mean in ggplot2 boxplot?

Is this anything like what you’re after? With stat_summary, as requested: # function for number of observations give.n <- function(x){ return(c(y = median(x)*1.05, label = length(x))) # experiment with the multiplier to find the perfect position } # function for mean labels mean.n <- function(x){ return(c(y = median(x)*0.97, label = round(mean(x),2))) # experiment with the … Read more