将 ggplot 图导出为 pdf 时出错:无效的字体类型

Error when exporting ggplot graphs to pdf: invalid font type

我在使用 ggsave() 保存绘图时遇到问题。我不断收到此错误:

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : invalid font type

我不明白为什么会出现此错误。我没有使用任何特殊类型的字体。我看了这些类似的问题:

但它们对我没有用。代码运行良好,绘图在 Rstudio 中完美打印!

示例数据:

final_df<-structure(list(orgid_sc = c("USGS-NJ", "USGS-NJ", "USGS-NJ", 
"USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", 
"21NJDEP1"), locid = c("USGS-01396030", "USGS-01396030", "USGS-01396030", 
"USGS-01396030", "USGS-01378560", "USGS-01378560", "USGS-01378560", 
"USGS-01378560", "USGS-01393400", "21NJDEP1-01396030"), stdate = structure(c(11359, 
11359, 11359, 11359, 16498, 16498, 16498, 16498, 12149, 12437
), class = "Date"), sttime = structure(c(34200, 34200, 34200, 
34200, 50400, 50400, 50400, 50400, 80280, 35700), class = c("hms", 
"difftime"), units = "secs"), Specific_conductance = c(7450, 
7450, 7300, 7300, 7190, 7190, 6960, 6960, 6080, 5550), valunit_sc = c("uS/cm @25C", 
"uS/cm @25C", "uS/cm @25C", "uS/cm @25C", "uS/cm @25C", "uS/cm @25C", 
"uS/cm @25C", "uS/cm @25C", "uS/cm @25C", "uS/cm @25C"), swqs_sc = c("FW2-NT", 
"FW2-NT", "FW2-NT", "FW2-NT", "FW2-NT", "FW2-NT", "FW2-NT", "FW2-NT", 
"FW2-NT", "FW2-NT"), WMA_sc = c(7L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, 
7L, 7L), year_sc = c(2001L, 2001L, 2001L, 2001L, 2015L, 2015L, 
2015L, 2015L, 2003L, 2004L), locid2 = c("01396030", "01396030", 
"01396030", "01396030", "01378560", "01378560", "01378560", "01378560", 
"01393400", "01396030"), HUC14 = c("HUC02030104050090", "HUC02030104050090", 
"HUC02030104050090", "HUC02030104050090", "HUC02030103180010", 
"HUC02030103180010", "HUC02030103180010", "HUC02030103180010", 
"HUC02030104020020", "HUC02030104050090"), HUC_name = c("Rahway River SB", 
"Rahway River SB", "Rahway River SB", "Rahway River SB", "Coles Brook / Van Saun Mill Brook", 
"Coles Brook / Van Saun Mill Brook", "Coles Brook / Van Saun Mill Brook", 
"Coles Brook / Van Saun Mill Brook", "Elizabeth R (Elizabeth CORP BDY to I-78)", 
"Rahway River SB"), orgid_tds = c("USGS-NJ", "USGS-NJ", "USGS-NJ", 
"USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", NA, "21NJDEP1"
), tds = c(3800, 4190, 3800, 4190, 3630, 3900, 3630, 3900, NA, 
3056), valunit_tds = c("mg/L", "mg/L", "mg/L", "mg/L", "mg/L", 
"mg/L", "mg/L", "mg/L", NA, "mg/L"), swqs_tds = c("FW2-NT", "FW2-NT", 
"FW2-NT", "FW2-NT", "FW2-NT", "FW2-NT", "FW2-NT", "FW2-NT", NA, 
"FW2-NT"), WMA_tds = c(7L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, NA, 7L
), year_tds = c(2001L, 2001L, 2001L, 2001L, 2015L, 2015L, 2015L, 
2015L, NA, 2004L), orgid = c("USGS-NJ", "USGS-NJ", "USGS-NJ", 
"USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", NA, "21NJDEP1"
), Chloride = c(2230, 2230, 2230, 2230, 2100, 2100, 2100, 2100, 
NA, 1409.3), valunit = c("mg/L", "mg/L", "mg/L", "mg/L", "mg/L", 
"mg/L", "mg/L", "mg/L", NA, "mg/L"), swqs = c("FW2-NT", "FW2-NT", 
"FW2-NT", "FW2-NT", "FW2-NT", "FW2-NT", "FW2-NT", "FW2-NT", NA, 
"FW2-NT"), WMA = c(7L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, NA, 7L), year = c(2001L, 
2001L, 2001L, 2001L, 2015L, 2015L, 2015L, 2015L, NA, 2004L), 
    combo = c("HUC02030104050090 Rahway River SB", "HUC02030104050090 Rahway River SB", 
    "HUC02030104050090 Rahway River SB", "HUC02030104050090 Rahway River SB", 
    "HUC02030103180010 Coles Brook / Van Saun Mill Brook", "HUC02030103180010 Coles Brook / Van Saun Mill Brook", 
    "HUC02030103180010 Coles Brook / Van Saun Mill Brook", "HUC02030103180010 Coles Brook / Van Saun Mill Brook", 
    "HUC02030104020020 Elizabeth R (Elizabeth CORP BDY to I-78)", 
    "HUC02030104050090 Rahway River SB")), .Names = c("orgid_sc", 
"locid", "stdate", "sttime", "Specific_conductance", "valunit_sc", 
"swqs_sc", "WMA_sc", "year_sc", "locid2", "HUC14", "HUC_name", 
"orgid_tds", "tds", "valunit_tds", "swqs_tds", "WMA_tds", "year_tds", 
"orgid", "Chloride", "valunit", "swqs", "WMA", "year", "combo"
), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))

代码:

formula1 <- y ~ x

correlation_theme<- theme_linedraw()+
  theme(plot.title=element_text(size=15, face="bold",vjust=0.5,hjust = 0.5),
        plot.subtitle = element_text(13,face = "bold",vjust=0.5,hjust=0.5),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        plot.background = element_blank(),
        panel.background = element_blank(),
        legend.position = c(0.5, 0.2),
        legend.background = element_blank(),
        legend.text=element_text(size=10, face="bold"))

corr_plot_map_way <- function(df, x, y, title,xlab,ylab) {
  x_var <- enquo(x)
  y_var <- enquo(y)

  ggplot(df, aes(x = !!x_var, y = !!y_var)) +
    geom_point(size = 2,alpha=0.5) +
    geom_smooth(method = "lm", se = FALSE,formula=formula1) +
    scale_x_continuous(limits = c(0, 6200), expand = c(0, 0)) +
    scale_y_continuous(limits = c(0, 2000), expand = c(0, 0)) +
    labs(
      title = title,
      #subtitle = paste(rlang::quo_text(x_var), "vs.", rlang::quo_text(y_var),"(1997-2018)"),
      subtitle = paste(xlab, "vs.", ylab,"(1997-2018)"),

      x=xlab,y=ylab)+
    stat_poly_eq(aes(label = paste(..eq.label.., sep = "~~~")), 
                 label.x.npc = 0.8, label.y.npc = 0.9,
                 eq.with.lhs = "italic(hat(y))~`=`~",
                 eq.x.rhs = "~italic(x)",
                 formula = formula1, parse = TRUE, size = 5) +
    stat_poly_eq(aes(label = paste(..rr.label.., sep = "~~~")), 
                 label.x.npc = 0.8, label.y.npc = 0.83,
                 formula = formula1, parse = TRUE, size = 5) +

    correlation_theme
}

nested_by_HUC14 <- 
  final_df %>%
  group_by(combo) %>% 
  nest()

plots<-nested_by_HUC14 %>%
  mutate(plot = map2(data, combo, ~ corr_plot_map_way(.x, Specific_conductance,Chloride, .y,xlab="Specific Conductance(µS/cm)",ylab="Chloride(mg/L)")),filename=paste0(combo,".pdf")) %>%
  select(filename,plot)

map2(paste0(plots$filename,".pdf"),plots$plot,ggsave)

我添加了所有代码,因为我不确定哪个部分可能导致此错误。抱歉拖了这么久!

您的主题中有错字:

plot.subtitle = element_text(size=13,face = "bold",vjust=0.5,hjust=0.5)

您需要 size=,因为 element_text 中的第一个参数是 family,而 13 不是有效的族。

此外,您的文件名不能包含正斜杠。

主要问题是:

  • combo 中的特殊字符 / 使文件名无效 -> 使用 str_replace/ 替换为 -

  • 您的 xlabylabsubtitle 中的特殊字符 -> 使用 substitute

library(rlang)
library(tidyverse)
library(ggpmisc)

formula1 <- y ~ x

corr_plot_map_way <- function(df, x, y, title, xlab, ylab) {
  x_var <- enquo(x)
  y_var <- enquo(y)

  ggplot(df, aes(x = !!x_var, y = !!y_var)) +
    geom_point(size = 2, alpha = 0.5) +
    geom_smooth(method = "lm", se = FALSE, formula = formula1) +
    scale_x_continuous(limits = c(0, 6200), expand = c(0, 0)) +
    scale_y_continuous(limits = c(0, 2000), expand = c(0, 0)) +
    labs(
      title = title,
      ### need to use substitute here ###
      subtitle = substitute(
        paste(x, " vs. ", y, " (1997-2018)"),
        list(x = xlab, y = ylab)
      )) +
    xlab(substitute(x, list(x = xlab))) +
    ylab(substitute(y, list(y = ylab))) +
    stat_poly_eq(aes(label = paste(..eq.label.., sep = "~~~")),
      label.x.npc = 0.8, label.y.npc = 0.9,
      eq.with.lhs = "italic(hat(y))~`=`~",
      eq.x.rhs = "~italic(x)",
      formula = formula1, parse = TRUE, size = 5
    ) +
    stat_poly_eq(aes(label = paste(..rr.label.., sep = "~~~")),
      label.x.npc = 0.8, label.y.npc = 0.83,
      formula = formula1, parse = TRUE, size = 5
    ) +
    correlation_theme
}

combo var

中删除 /
final_df <- final_df %>%
  mutate_at(vars(contains("combo")), funs(str_replace(., "/", "-")))

nested_by_HUC14 <-
  final_df %>%
  group_by(combo) %>%
  nest()

设置xlabylab绘图

xlab <- "Specific Conductance" ~ (mu / cm)
ylab <- "Chloride (mg/L)"

plots <- nested_by_HUC14 %>%
  mutate(
    plot = map2(data, combo, ~ corr_plot_map_way(.x, Specific_conductance, Chloride, .y,
      xlab = xlab,
      ylab = ylab
    )),
    filename = paste0(combo, ".pdf")
  ) %>%
  select(filename, plot)

plots$plot[[1]]

保存为 PDF 并使用 cairo_pdf 嵌入字体

map2(plots$filename, plots$plot, ggsave, device = cairo_pdf,
     width = 6, height = 4, units = "in")

编辑:了解更多关于在 R 中使用数学符号的信息 here & here