0

I am trying to add labels to my plot that contain subscripts. The code runs fine in R studio, but when exporting the figure using ggsave() I get the following error:

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
  Metric information not available for this family/device

Here is my data:

dput(LLTemp_wide)
structure(list(Temp = c(-15, -20, -22.5, -25, -30), Alive = c(5L, 
4L, 2L, 1L, 0L), Dead = c(0L, 2L, 4L, 5L, 5L), Prop_Survival = c(1, 
0.6666, 0.3333, 0.1666, 0)), class = "data.frame", row.names = c(NA, 
-5L))

The packages in using:

library(plyr)
library(MASS)
library(ggPredict)
library(dplyr)
library(ggplot2)

And my code:

# theme adjustments
Alex_Theme = theme_bw() +
  theme(plot.title = element_text(hjust = 0.5, face='plain', size = 12)) +
  theme(plot.title = element_text(vjust=-1.8)) +
  theme(plot.subtitle=element_text(size=10, hjust=0.5, face="italic", color="black")) +
  theme(legend.position="none") +
  theme(panel.border = element_rect(fill=NA, colour = "black", size=0.5)) +
  theme(axis.text = element_text(face = "plain", size = 10)) +
  theme(axis.title.x = element_text(margin = margin(t = 8, r = 20, b = 0, l = 0))) +
  theme(axis.title.y = element_text(margin = margin(t = 8, r = 6, b = 0, l = 0))) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(axis.title = element_text(face="plain", size = 10))

ggplot(LLTemp_wide, aes(x = Temp, y = Prop_Survival)) +
  Alex_Theme +
  #ggtitle("UIC: Lower lethal temperature") +
  #theme(plot.title = element_text(size = 10, vjust = 0.5)) +
  xlab("Temperature (°C)") +
  ylab("Survival Probibility") +
  geom_segment(aes(x = -21.4917, y = -Inf, xend =  -21.4917, yend = 0.5), linetype = 2, color = 'grey50') + # LT50 vertical line
  geom_segment(aes(x = -Inf, y = 0.5, xend = -21.4917, yend = 0.5), linetype = 2, color = 'grey50') + # LT50 horizontal line
  geom_segment(aes(x = -25.35566, y = -Inf, xend =  -25.35566, yend = 0.1), linetype = 2, color = 'grey50') + # LT10 vertical line
  geom_segment(aes(x = -Inf, y = 0.1, xend = -25.35566, yend = 0.1), linetype = 2, color = 'grey50') +  # LT10 horizontal line
  geom_segment(aes(x = -17.62774, y = -Inf, xend =  -17.62774, yend = 0.9), linetype = 2, color = 'grey50') + # LT90 vertical line
  geom_segment(aes(x = -Inf, y = 0.9, xend = -17.62774, yend = 0.9), linetype = 2, color = 'grey50') +  # LT90 horizontal line
  stat_smooth(method = "glm", 
              method.args = list(family = quasibinomial(link = 'logit')),
              se = FALSE,
              colour = "red") +
  geom_point() +
  annotate(geom = "text", x = -29, y = 0.55, size = 3, parse = TRUE, label = as.character(expression(paste(LT[50], "= -21.5 °C")))) +
  annotate(geom = "text", x = -33, y = 0.15, size = 3, parse = TRUE, label = as.character(expression(paste(LT[90], "= -25.4 °C")))) +
  annotate(geom = "text", x = -25, y = 0.95, size = 3, parse = TRUE, label = as.character(expression(paste(LT[10], "= -17.6 °C")))
  )

  ## Save Plot
ggsave("ALB_Lethal_Temp.png", width = 3.5, height = 4, type = "cairo-png")

Here is a screenshot of the plot in the console (seems to be working fine...): enter image description here

And what I end up with after ggsave() export: enter image description here

If I remove the expression from the label and just print text, the ggsave() export works fine. Is this a ggsave issue or some issue with my code?

Alex
  • 261
  • 2
  • 5
  • 11
  • 1
    Works for me on Linux. Are you using Windows ? – Stéphane Laurent Oct 18 '19 at 14:42
  • maybe using cairo device helps. e.g. [this thread](https://stackoverflow.com/questions/12768176/unicode-characters-in-ggplot2-pdf-output/12775087) or [this here](https://www.andrewheiss.com/blog/2017/09/27/working-with-r-cairo-graphics-custom-fonts-and-ggplot/) – tjebo Oct 18 '19 at 16:12
  • pdf works fine for me, but labels are too big then. Also just removing the `,type="cairo-png"` lets me save it on windows without issues. ggplot knows, that you want a png from the file extension in the file name – TobiO Oct 18 '19 at 22:13

0 Answers0