【问题标题】:Distorted image when adding text outside ggplot facets with gridExtra使用 gridExtra 在 ggplot 构面之外添加文本时图像失真
【发布时间】:2018-11-29 06:59:54
【问题描述】:

我正在尝试使用facet_wrap 在使用 ggplot 获得的绘图区域之外添加文件名。我很确定我在这篇文章中找到了解决方案: Add filename or other annotation to ggplot figures。但是,将解决方案应用于我的问题会产生扭曲的图像。

生成这个的代码在这里:

require("gridExtra")
library(tidyverse)

df <- data.frame(x =runif(100, 1, 10),
             y = runif(100, 1, 10),
             myfacet = c("one", "two"))
p <- ggplot(data = df,
        aes(x = x,
            y = y)) +
geom_point() +
facet_wrap(~myfacet)

print(p)
script.name <- "myscript.R"
sub.label = textGrob(script.name, 
                 gp=gpar(fontsize=6),
                 x = unit(1, "npc"),
                 hjust = 1,
                 vjust = 1)
ggsave(filename="../plots/myplot.png",
   plot = arrangeGrob(p,
                      sub = sub.label,
                      clip = FALSE))

如果我只是使用

ggsave(filename="../plots/myplot2.png",
   plot = p)

我得到以下图像:

请注意,我需要一个在各个方面都有效的解决方案。 任何人都可以提供有关正在发生的事情的提示吗?谢谢!

【问题讨论】:

    标签: r ggplot2 gridextra


    【解决方案1】:
    grid.arrange(p, bottom = sub.label)
    

    【讨论】:

    • 还有没有办法将 sub.label 从情节边缘稍微移开?如果标签中有下划线,否则很难阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    相关资源
    最近更新 更多