ggsave () не работает, когда метка слоя annotate () имеет подписки - PullRequest
0 голосов
/ 18 октября 2019

Я пытаюсь добавить метки к своему графику, которые содержат подписки. Код прекрасно работает в R studio, но при экспорте рисунка с помощью ggsave () я получаю следующую ошибку:

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

Вот мои данные:

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

Пакеты в использовании:

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

И мой код:

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

Вот скриншот графика в консоли (вроде нормально работает ...): enter image description here

И что я получаю после экспорта ggsave (): enter image description here

Если я удаляю выражение из метки и просто печатаю текст,ggsave () экспорт работает нормально. Это проблема ggsave или какая-то проблема с моим кодом?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...