gpt4 book ai didi

r - ggplot2,将两个比例应用于同一个图?自上而下的条形图

转载 作者:行者123 更新时间:2023-12-04 17:00:17 25 4
gpt4 key购买 nike

看这里的情节:

enter image description here
(来自 here)

如何使用 ggplot2 重现条形图的上部和下部?

例如,我可以用

ggplot(data.frame(x=rnorm(1000, 5)), aes(x=x)) + geom_bar() + scale_y_reverse()

但是现在如果我添加任何其他 geom_ ,如另一个 geom_bar() y 的比例是相反的。是否可以申请 scale_y_reverse()仅限特定的 geom_ ?

最佳答案

另一种选择是制作两个单独的图并将它们与 arrangeGrob 结合起来。来自 gridExtra 包裹。在玩过情节边缘之后,您可以得出一些看起来不错的东西。

library(gridExtra)
library(ggplot2)

set.seed(100)
p2 <- ggplot(data.frame(x=rnorm(1000, 5)), aes(x=x)) + geom_bar() + theme(plot.margin=unit(c(0,0,0,0), 'lines'))
p1 <- p2 + scale_y_reverse() +
theme(plot.margin=unit(c(0, 0, -.8, 0), 'lines'), axis.title.x=element_blank(),
axis.text.x=element_blank(), axis.ticks.x=element_blank())

p <- arrangeGrob(p1, p2)
print(p)

enter image description here

关于r - ggplot2,将两个比例应用于同一个图?自上而下的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24916873/

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