gpt4 book ai didi

从 ggplot2 的 Facet 中删除未使用的因素

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

我试图找出一种巧妙的方法来从 ggplot2 的一个方面中删除未使用的因素。这是一个最小的例子

# DUMMY DATA
mydf = data.frame(
x = rpois(6, 25),
y = LETTERS[1:6],
cat = c(rep('AA', 3), rep('BB', 3)))

# PLOT IT!
p0 = ggplot(mydf, aes(x = x, y = y)) +
geom_point() +
facet_wrap(~ cat, ncol = 1)

从下图中,您可以看到因子 D、E 和 F 绘制在面 AA 中,尽管没有相应的数据。我想要的是一种从面 AA 中消除 {D, E, F} 和类似地从面 BB 中消除 {A, B, C} 的方法。

有没有一种巧妙的方法可以做到这一点,或者甚至可以接受黑客攻击。

enter image description here

最佳答案

我认为您只需要 scales = "free_y" :

p0 = ggplot(mydf, aes(x = x, y = y)) +
geom_point() +
facet_wrap(~ cat, ncol = 1,scales = "free_y")

p0

enter image description here

关于从 ggplot2 的 Facet 中删除未使用的因素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7031821/

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