gpt4 book ai didi

R:如何相对于 x 轴散布(抖动)点?

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

我在 R 中有以下代码片段:

dat <- data.frame(cond = factor(rep("A",10)), 
rating = c(1,2,3,4,6,6,7,8,9,10))
ggplot(dat, aes(x=cond, y=rating)) +
geom_boxplot() +
guides(fill=FALSE) +
geom_point(aes(y=3)) +
geom_point(aes(y=3)) +
geom_point(aes(y=5))

这个特定的代码片段会生成一个箱线图,其中一个点越过另一个点(在上述情况下,一个点 3 越过另一个点 3)。

如何移动点 3 以使该点保持在 y 轴上的相同位置,但在 x 轴上向左或向右略微移动?

最佳答案

这可以通过使用 position_jitter 来实现。功能:

geom_point(aes(y=3), position = position_jitter(w = 0.1, h = 0))

更新:
要仅绘制提供的三个点,您可以构建一个新的数据集并绘制:
points_dat <- data.frame(cond = factor(rep("A", 3)), rating = c(3, 3, 5))                  
ggplot(dat, aes(x=cond, y=rating)) +
geom_boxplot() +
guides(fill=FALSE) +
geom_point(aes(x=cond, y=rating), data = points_dat, position = position_jitter(w = 0.05, h = 0))

关于R:如何相对于 x 轴散布(抖动)点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31405985/

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