0

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?

hypehuman
  • 1,290
  • 2
  • 18
  • 37
  • 3
    Try saving the plot and then printing it explicitly - `p <- ggplot()+...`, then `print(p)` – Andrew Gustar Mar 29 '23 at 16:20
  • @AndrewGustar That worked! Are you able to explain why? – hypehuman Mar 29 '23 at 16:23
  • 1
    I think doing `ggplot` in a function is in the wrong environment to produce the output you would expect if you do it in the console. The `print` forces it to output the plot to the console (or device). I'm not sure of all the details, but it works! – Andrew Gustar Mar 29 '23 at 16:29
  • 2
    @SamR Yes, thank you. For reference, the relevant link was https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f "When you use these functions interactively at the command line, the result is automatically printed, but in source() or inside your own functions you will need an explicit print() statement." – hypehuman Mar 29 '23 at 16:33
  • "Normal" base R graphics operate on the general principle where the functions called actually draw the elements on an output device (e.g. `points()`, `lines()`). grid graphics, on which ggplot2 is built, operates differently, where the functions build an actual R object that describes the plot, and then `print()` tells R to actually draw it. The reason why this catches people is that at the console prompt, when you type the name of an object, that `print()` method is being called automaticcally. – joran Mar 29 '23 at 16:33
  • This behavior of grid graphics is similar to most other R objects. e.g., compare `f1 <- function() { 1; 2; 3 }; f1()` and `f2 <- function() { print(1); print(2); print(3) }; f2()`. – zephryl Mar 29 '23 at 16:35
  • And does this also apply for the last line of a function that is called from the console prompt? Is that why I was getting some graphs but not others? – hypehuman Mar 29 '23 at 16:35

0 Answers0