## 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

## I have 7 different data points for virus concentration collected at 3 different time points. How do I graph this with error bars in R?

I would suggest you next approach. If you want error bars you can compute it based on mean and standard deviation. In the next code is sketched the way to do that. I have used one standard deviation but you can set any other value. Also as you want to see different samples, I have … Read more

## Apply several summary functions (sum, mean, etc.) on several variables by group in one call

You can do it all in one step and get proper labeling: > aggregate(. ~ id1+id2, data = x, FUN = function(x) c(mn = mean(x), n = length(x) ) ) # id1 id2 val1.mn val1.n val2.mn val2.n # 1 a x 1.5 2.0 6.5 2.0 # 2 b x 2.0 2.0 8.0 2.0 # 3 … 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(samp.data\$fullname) ## Reorder fullname based on the the sum of the other columns samp.data\$fullname <- reorder(samp.data\$fullname, rowSums(samp.data[-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(as.data.frame(unclass(textcnt(x,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

## Get all diagonal vectors from matrix

A <- matrix(1:16, 4) # create an indicator for all diagonals in the matrix d <- row(A) – col(A) # use split to group on these values split(A, d) # # \$`-3` # [1] 13 # # \$`-2` # [1] 9 14 # # \$`-1` # [1] 5 10 15 # # \$`0` # [1] … Read more