我想保存一个 ggplot 对象,并将 Cramérs V 作为图例名称。当我运行 ggsave() 函数时,出现以下错误:
Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
Metric information not available for this family/device
只有当我用重音 aigu 写 Cramér 这个词时,我才会收到此错误,没有重音,一切正常。
下面是一个产生错误的小示例:
library(ggplot2)
df <- mtcars
barplot <- ggplot(df,aes(x=mpg,y=cyl,fill=disp))+
scale_fill_gradient(name = expression(Cramérs ~ italic(V)))+
geom_bar(stat='identity')
barplot
# save as cairo-png
ggsave(barplot, filename = "./plot.png",type="cairo",bg="transparent",dpi=300,unit="cm",width=40,height=20)
如何让 ggsave 函数保存图例名称中带有重音符号的对象?
像这样保存它:
ggplot(mtcars,aes(x=mpg,y=cyl,fill=disp))+
scale_fill_gradient(name = expression(Cramérs ~ italic(V)))+
geom_bar(stat='identity')
#guides(fill=guide_legend(title=expression(Cramérs ~ italic(V))))
#add relevant preferences in the ggsave call. Key is to change type
ggsave(filename="myplot.png",last_plot(),type=cairo_ps())