I wrote/adapted the following code, and it works great. I get nine beautiful graphs in Devices 2-10.
library(tidyverse)
trialData <- as_tibble(read.csv(file = 'analysis by trial.csv'))
probeData <- as_tibble(read.csv(file = 'analysis by probe.csv'))
plotOverVersions <- function(df, metric, projectFilter) {
if (projectFilter=="")
{
filtered <- df
}
else
{
filtered <- df %>% filter(Project==projectFilter)
}
errbar_lims <- filtered %>% group_by(Version) %>% summarize(
mean=mean({{metric}}),
se=sd({{metric}})/sqrt(n()),
upper=mean+(se),
lower=mean-(se)
)
dev.new()
ggplot() +
geom_violin(data=filtered, aes(x=Version, y={{metric}}, fill=Version, color=Version)) +
geom_point(data=errbar_lims, aes(x=Version, y=mean), size=3) + # show the mean as a point
geom_errorbar(data=errbar_lims, aes(x=Version, ymax=upper, ymin=lower), stat='identity', width=1) + # show the SEM as whiskers
theme_minimal() +
#coord_cartesian(ylim = quantile({{metric}}, c(0.01, 0.99))) + # zoom in on the middle 98% of the data
expand_limits(y=0) # expand to show y=0 if necessary
}
plotOverVersions(trialData, Runtime, "")
plotOverVersions(trialData, end.stuck.ContextIdle, "")
plotOverVersions(probeData, average.delay.Background, "")
plotOverVersions(trialData, Runtime, "A")
plotOverVersions(trialData, end.stuck.ContextIdle, "A")
plotOverVersions(probeData, average.delay.Background, "A")
plotOverVersions(trialData, Runtime, "B")
plotOverVersions(trialData, end.stuck.ContextIdle, "B")
plotOverVersions(probeData, average.delay.Background, "B")
The last three paragraphs are repetitive, though, so I thought I'd clean them up by replacing them with this:
plotAllMetrics <- function(projectFilter) {
plotOverVersions(trialData, Runtime, projectFilter)
plotOverVersions(trialData, end.stuck.ContextIdle, projectFilter)
plotOverVersions(probeData, average.delay.Background, projectFilter)
}
plotAllMetrics("")
plotAllMetrics("A")
plotAllMetrics("B")
However, now I only get three beautiful graphs (in Devices 4, 7, and 10), and six empty gray canvases (in Devices 2-3, 5-6, and 8-9). It looks like only the third line in each method call is resulting in a successful graph.
Why did organizing this code into a function result in some of the graphs not rendering?