使用ggsave导出分面图显示异常,RStudio手动导出正常求助
批量导出分面图时ggsave显示异常的问题
需要批量导出大量图表,无法手动逐个操作。但使用ggsave及其他替代方法导出时,分面图显示异常,调整尺寸、缩放等参数均无效果。
复现代码
# 模拟数据集 question <- c("Q1. A very long text like this one is hard to use as a label.", "Q2. A very long text like this one is hard to use as a label.", "Q3. A very long text like this one is hard to use as a label.", "Q4. A very long text like this one is hard to use as a label.", "Q5. A very long text like this one is hard to use as a label.", "Q6. A very long text like this one is hard to use as a label.", "Q7. A very long text like this one is hard to use as a label.", "Q8. A very long text like this one is hard to use as a label.", "Q9. A very long text like this one is hard to use as a label.", "Q10. A very long text like this one is hard to use as a label.", "Q11. A very long text like this one is hard to use as a label.", "Q12. A very long text like this one is hard to use as a label.", "Q13. A very long text like this one is hard to use as a label.", "Q14. A very long text like this one is hard to use as a label.", "Q15. A very long text like this one is hard to use as a label.") variable <- c("V1","V2") set.seed(42) response <- round(runif(n = length(question) * length(variable), min = 1, max = 10), 0) # 创建数据框 df_example <- expand.grid(Question = question, Variable = variable) df_example$Response <- rep(response, length.out = nrow(df_example)) # 绘制柱状图 p <- ggplot(df_example, aes(y = Variable, x = Response, fill = Variable)) + geom_bar(stat = "identity", position = "dodge") + facet_wrap(~Question, ncol = 1, strip.position = "left") + theme_minimal() + theme( legend.position = "none", axis.title.x = element_blank(), axis.title.y = element_blank(), strip.text.y.left = ggtext::element_textbox_simple( hjust = 0, padding = unit(c(0, 20, 0, 0), "pt") ), strip.placement = "outside" ) # 使用ggsave导出 ggsave("test_plot", plot = p, device = "jpeg", scale = 1, width = 26.6, height = 15, units = "cm", dpi = 320)
导出效果对比
- RStudio手动导出的正常图表:分面左侧的长问题文本完整展示,每个分面布局合理,内容清晰可见
- ggsave导出的异常图表:分面左侧的长文本被截断,图表布局错乱,部分内容无法正常显示
内容的提问来源于stack exchange,提问作者Bart Yarborough
相关产品推荐
相关产品推荐

