【问题标题】:ggforce facet_zoom how to annotate only zoomed graphicggforce facet_zoom 如何仅注释缩放的图形
【发布时间】:2017-09-29 20:00:16
【问题描述】:

我想仅在放大区域中对图表进行注释,以提供有关突出显示数据的详细信息。

在以下示例代码中,想法是仅在放大区域中显示文本“仅缩放”。

require(ggplot2)
require(ggforce)
ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
    geom_point() +
    facet_zoom(x = Species == "versicolor") + 
    annotate("text", x=4, y=2, label="zoom only")

仅缩放文本(预期状态):

两者中的文本(当前状态):

有没有办法做到这一点?

【问题讨论】:

  • 请参阅my answer 类似的问题。关键是在数据中创建一个列来表示要绘制到哪个面板,并将该列的名称传递给facet_zoomzoom.data 参数。
  • @ArtemSokolov zoom.data 是最近才添加的。感谢您的更新。

标签: r ggplot2 ggforce


【解决方案1】:

我们可以深入研究对象,并将该文本设置为透明 (alpha = 0):

p <- ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
  geom_point() +
  facet_zoom(x = Species == "versicolor") + 
  annotate("text", x=4, y=2, label="zoom only")

pb <- ggplot_build(p)
pb$data[[2]][1, 'alpha'] <- 0
pg <- ggplot_gtable(pb)
plot(pg)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多