gpt4 book ai didi

r - 添加另一个 ggplot 到/注释 facet_wrap 图

转载 作者:行者123 更新时间:2023-12-01 08:29:38 26 4
gpt4 key购买 nike

我有一个相当复杂的 facet_wrap 等值线 map ,它们共享一个共同的图例并将它们分成单独的图。我想将另一个非常简单的条形图添加到未使用的构面区域中。我想我可以使用这里看到的 annotation_custom:

https://github.com/hadley/ggplot2/wiki/Mixing-ggplot2-graphs-with-other-graphical-output

p = qplot(1:10, 1:10) + theme_bw()
g = ggplotGrob(qplot(1, 1))
p + annotation_custom(grob = g, xmin = 2, xmax = 7, ymin = 6, ymax = 10)

但即使在使用 x/y-min/max 时,这似乎也无法正确调整大小。似乎要在每个方面进行注释。不是我想要的。

将每个 facet 分成一个单独的图并使用 gridExtra::grid.arrange 会很困难,因为每个 facet 共同生成的共享梯度图例。

plot1 <- ggplot(mtcars, aes(factor(mtcars$gear))) + 
geom_bar() +
facet_wrap(~cyl, ncol=2) +
coord_flip()


plot2 <- ggplot(mtcars, aes(as.factor(am))) +
geom_bar(aes(fill=factor(cyl)))

这失败了(每个方面的情节):

g <- ggplotGrob(plot2)

plot1 + annotation_custom(grob = g, xmin = 0, xmax = 1, ymin = 0, ymax = 1)

想要的外观:

enter image description here

最佳答案

g1 <- ggplotGrob(plot1)
g2 <- ggplotGrob(plot2)

g1 <- gtable::gtable_add_grob(g1, g2, t = 8, l=7)
grid.newpage()
grid.draw(g1)

关于r - 添加另一个 ggplot 到/注释 facet_wrap 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22584334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com