用小平面在ggplot中注释一次绘图区域

时间:2018-11-06 07:41:01

标签: r ggplot2

我想在多面ggplot中的绘图区域之外添加注释。我可以得到想要的注释,但是每个方面都会重复该注释。如何使该注释仅显示一次?

例如,要在左上角注释一次“ XX”,我可以使用:

    // Prevent loading file when delete file is used.
    $('.collapsible-body .action-icon').on('click', function(e) {
        console.log('testings')
        e.stopPropagation();
    });

    $('#DOMWindow .action-icon').on('click', function(event) {
        event.preventDefault();
        event.stopPropagation();
    });

annotated plot

但这实际上将其注释在每个构面的左上方。

如何使它只出现一次?

4 个答案:

答案 0 :(得分:3)

实际上非常容易,只需具有标签的 vector ,其中您不想绘制的标签就是空字符串""

library("ggplot2")

ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  annotate("text", x = -20, y = 36, label = c("XX", "", "")) +
  facet_grid(.~cyl ) + 
  coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off")

enter image description here

答案 1 :(得分:2)

您可以使用tag中的labs()在图形上放置单个标签标签。

ggplot(mtcars, aes(x = hp, y = mpg)) +
     geom_point() +
     facet_grid(.~cyl ) + 
     labs(tag = "XX") +
     coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off")

enter image description here

不过,此默认设置为“左上”,这可能不是您想要的。您可以使用主题元素plot.tag.position来移动它,或者将其作为坐标(在绘图空间中介于0和1之间)或像"topright"这样的字符串。

ggplot(mtcars, aes(x = hp, y = mpg)) +
     geom_point() +
     facet_grid(.~cyl ) + 
     labs(tag = "XX") +
     coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off") +
     theme(plot.tag.position = c(.01, .95))

enter image description here

答案 2 :(得分:1)

使用geom_text

dummy <- data.frame(cyl = c(4), l = c("XX"), stringsAsFactors = F)

ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  geom_text(data=dummy, aes(label=l), x = -20, y = 36) +
  facet_grid(.~cyl ) + 
  coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off")

enter image description here

答案 3 :(得分:1)

或者,软件包cowplot具有便捷的注释功能pg_retore -U pox4 --data-only --dbname pox4_production pox4_product_order_rows.pg 。与draw_label()结合使用时,可以在画布/工作表上的任意位置注释,坐标范围为0到1(相对于整个画布)。函数ggdraw()在幕后使用cowplot::draw_label()

ggplot2::annotation_custom()

reprex package(v0.2.1)于2019-01-14创建

相关问题