在 R 中的绘图区域之外添加脚注引用? [英] Add a footnote citation outside of plot area in R?

查看:36
本文介绍了在 R 中的绘图区域之外添加脚注引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我用 R 生成的 3 面板分面网格图中添加脚注引用.这是一个脚注,用于注明数据源.理想情况下,我希望将它放在所有三个轴的下方和外部——最好在左下方.

我正在使用 ggplot2ggsave().这意味着我不能使用基于 grid.text() 的解决方案,因为它只能在 x11() 窗口上绘制,而不能添加到 ggplot目的.

改用 png() ...code... dev.off() 似乎不是一个选项,因为我需要 ggsave 的调整大小参数,并发现此命令可以生成更好、更清晰的打印件(也更快,因为我没有打印到屏幕上).

这是我的基本代码:

p1 <- ggplot(data, aes(date, value))facet_grid(variable ~ .) + geom_point(aes(y =value), size=1) +theme_bw() +选项(标题=我的标题)打印(p1)ggsave("FILE.png",width=mywidth, height=myheight, p1, dpi=90)

我试过了:

p1 <- ggplot(data, aes(date, value))facet_grid(variable ~ .) + geom_point(aes(y =value), size=1) +theme_bw() +选项(标题=我的标题)打印(p1)grid.text(unit(0.1,"npc"),0.025,label = "数据由我提供")grid.gedit("GRID.text", gp=gpar(fontsize=7))ggsave("FILE.png",width=mywidth, height=myheight, p1, dpi=90)

这适当地将脚注放在 x11() 显示的左下角,在绘图的外部,但不幸的是,由于它没有应用于 p1 对象,所以它不会被 ggsave 命令保存.

我也试过:

p1 <- ggplot(data, aes(date, value))facet_grid(variable ~ .) + geom_point(aes(y =value), size=1) +theme_bw() +选项(标题=我的标题)+annotate("text", label = "Footnote", x = 0, y = 10, size = 5, color = "black") +打印(p1)ggsave("FILE.png",width=mywidth, height=myheight, p1, dpi=90)

使用 ggsave 成功打印,但存在以下问题:

  • 在 3 个方面中的每一个方面重复 3 次,而不是 1 次.
  • 它包含在情节中,而不是在情节之外.
  • 文本难以放置---似乎使用了绘图单位(我的 x 轴是日期,因此 0 表示 1970 年左右).
  • 尽管我设置了大小参数,但文本大小似乎没有改变.

当我探索这个时的几个相关链接......

  • 请注意,此解决方案与最近添加到 ggplot2 的标题参数有些互补,因为这里的 textGrob 可以相对于整个图形对齐,而不仅仅是绘图面板.

    I'd like to add a footnote citation to my 3-panel facet grid plot produced in R. It's a footnote to credit the data source. I'd ideally like to have it below and external to all three axes---preferably in the lower left.

    I'm using ggplot2 and also ggsave(). This means I can't use grid.text()-based solutions, because that only draws on the x11() window, and can't be added to the ggplot object.

    Using instead png() ...code... dev.off() does not appear to be an option because I need ggsave's resizing parameters, and find this command produces better, clearer prints (that are also much faster, because I'm not printing to the screen).

    Here's my basic code:

    p1 <- ggplot(data, aes(date, value))
        facet_grid(variable ~ .) + geom_point(aes(y =value), size=1) + 
        theme_bw() +
            opts(title=mytitle)
    print(p1)
    ggsave("FILE.png",width=mywidth, height=myheight, p1, dpi=90)
    

    I've tried:

    p1 <- ggplot(data, aes(date, value))
        facet_grid(variable ~ .) + geom_point(aes(y =value), size=1) + 
        theme_bw() +
            opts(title=mytitle)
    print(p1)
    grid.text(unit(0.1,"npc"),0.025,label = "Data courtesy of Me")
    grid.gedit("GRID.text", gp=gpar(fontsize=7))
    ggsave("FILE.png",width=mywidth, height=myheight, p1, dpi=90)
    

    This appropriately puts the footnote in the lower left corner on the x11() display, external to the plots, but unfortunately, since it isn't applied to the p1 object, it isn't saved by the ggsave command.

    I've also tried:

    p1 <- ggplot(data, aes(date, value))
        facet_grid(variable ~ .) + geom_point(aes(y =value), size=1) + 
        theme_bw() +
        opts(title=mytitle) +
    annotate("text", label = "Footnote", x = 0, y = 10, size = 5, colour = "black") +
    print(p1)
    ggsave("FILE.png",width=mywidth, height=myheight, p1, dpi=90)
    

    This successfully prints using ggsave, however it has the following problems:

    • It is repeated 3 times, in each of the 3 facets, rather than 1 time.
    • It is contained within the plots, rather than external to them.
    • Text is difficult to place---seems to be using plot units (my x-axis is date, so 0 puts it around 1970).
    • The text size doesn't seem to change despite my size parameter.

    A couple of related links from when I explored this...

    解决方案

    library(gridExtra)
    library(grid)
    library(ggplot2)
    
    g <- grid.arrange(qplot(1:10, 1:10, colour=1:10) + labs(caption="ggplot2 caption"), 
                  bottom = textGrob("grid caption", x = 1, 
                                    hjust = 1, gp = gpar(fontface = 3L, fontsize = 9)))
    ggsave("plot.pdf", g)
    

    Edit: note that this solution is somewhat complementary to the recent caption argument added to ggplot2, since the textGrob can here be aligned with respect to the whole figure, not just the plot panel.

    这篇关于在 R 中的绘图区域之外添加脚注引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆