I have an issue, where the first plot of a ggplot script in a RStudio session is really quick (~ 2-3 seconds) and all following plots in the same session, using exactly the same script, take significantly longer (~ 10-20 seconds). I also often need to reload the plot to get it correctly plotted.
Sometimes I also get the warning message:
In grid.Call.graphics(C_setviewport, vp, TRUE) : reached elapsed time limit
or In Summary.unit(list(list(2.2, NULL, 8L), list(1, list(list(1, list( : reached elapsed time limit
, although the plot still renders after pressing the "refresh current plot" button.
I guess there is an issue with calling some function repeatedly, but I can't figure out which one it would be.
Here is the script I use:
library(ggplot2)
library(tidyverse)
library(showtext)
library(jpeg)
library(patchwork)
#font
font_add("frutiger", regular = "Schrift/frutigerltcom-light.ttf", italic = "Schrift/frutigerltcom-lightitalic.ttf")
showtext_auto()
#colors
i_palette <- c('#ab9c7d', '#235b7f', '#a6aa18', '#338598', '#12c1cd', '#b3d222')
#read data
data <- read.csv(file = "Input_CSV/cond.csv", sep = ";")
#GEOMETRY
g <- ggplot(data,
aes(fill = structure,y=Mittelwert, x=fct_inorder(paste))
) +
geom_hline(yintercept = 3, linetype = 2) +
geom_bar(position = "dodge", stat = "identity") +
geom_errorbar(aes(ymin=Mittelwert-Stdabw, ymax=Mittelwert+Stdabw), width=.2, position=position_dodge(.9))
#LABEL
l <- g +
labs(x = "paste",
y = expression(paste("conductance in ",10^{6}," S/m")),
)
#SCALE
s <- l +
scale_y_continuous(breaks = c(0,3,5,10,15,20)) +
scale_x_discrete(labels = c("1","2","3","4")) +
scale_fill_manual(values = i_palette) +
scale_colour_manual(values = i_palette)
#COORDINATES
c <- s +
coord_cartesian (xlim = c(1, 4), clip="off")
#ANNOTATE
a <- c +
annotate("text", x = 5, y = 3, label = "spec", size = 3)
#THEME
graph1 <- a +
theme_bw() +
theme(text = element_text('frutiger', size=14),
panel.grid.minor = element_blank()
)
#ADD LOGO:
path1 <- "./logo_300px.jpg"
img <- readJPEG(path1, native = TRUE)
img_graph <- graph1 + inset_element(p=img,left = 0.83,bottom = 0,right = 0.97,top = .1, align_to = 'full')
img_graph
showtext_auto(FALSE)
Adding showtext_auto(FALSE) already cut one or two seconds of the time and I suspected the delay is coming from the showtext package, but removing the font_add() and showtext_auto() command for the second run doesn't help.
I also tried:
- Clearing the workspace
- removing the library() calls after the first run
- using print(img_graph) or plot(img_graph) instead of just calling img_graph
Does anyone have an idea to solve this? I already tried to google some ggplot2 script best practices and other things, but I couldn't find any helpful source.
Thanks alot for your help!