我在使用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)
我添加了所有代码,因为我不确定哪个部分可能导致此错误。抱歉,这么长!
答案 0 :(得分:2)
主要问题是:
/
中的特殊字符combo
使文件名无效->使用str_replace
将/
替换为-
您的xlab
,ylab
,subtitle
->中的特殊字符使用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
final_df <- final_df %>%
mutate_at(vars(contains("combo")), funs(str_replace(., "/", "-")))
nested_by_HUC14 <-
final_df %>%
group_by(combo) %>%
nest()
设置xlab
和ylab
进行绘图
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")
答案 1 :(得分:1)
这是您的主题中的错字:
plot.subtitle = element_text(size=13,face = "bold",vjust=0.5,hjust=0.5)
您需要size=
,因为element_text
中的第一个参数是family
,并且13
不是有效的族。
此外,您的文件名不能包含正斜杠。