gpt4 book ai didi

r - 沿x轴按组划分geom_point点

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

这使我无所适从,而且我确定我缺少一些简单的东西。任何帮助,将不胜感激。

我希望将红色和蓝色点分开,每个点都在第二张图像中的相应箱形图上进行设置,但在第一张图像中使用数字x轴。

df <- data.frame(x = rep(c(1, 2, 10), each = 20), 
g = rep(c("A", "B"), times = 30),
y = c(rnorm(60, 0, 1)))

# OK - boxplot by x and g
ggplot(df, aes(y = y, x = x, fill = g, color = g, group = interaction(x, g))) +
geom_boxplot()

# Not OK. The dots are only grouped by x, not g
ggplot(df, aes(y = y, x = x, fill = g, color = g, group = interaction(x, g))) +
geom_point()

# I want the points to correctly overlay the boxplots
ggplot(df, aes(y = y, x = x, fill = g, color = g, group = interaction(x, g))) +
geom_boxplot(alpha = 0.1) +
geom_point()

enter image description here

(我已通过在x上刻面来修复它,但我希望轴为数字以反射(reflect)正确的缩放比例)
   ggplot(df, aes(y = y, x = g, fill = g, color = g, group = interaction(x, g))) +
geom_boxplot(alpha = 0.1) +
geom_point() +
facet_wrap(~x)

enter image description here

最佳答案

您可以在position=position_dodge(...)中使用geom_point

ggplot(df, aes(y = y, x = x, fill = g, color = g, group = interaction(x, g))) +
geom_boxplot(alpha = 0.1, width=0.75) +
geom_point(position = position_dodge(width=0.75))

我还为 geom_boxplot定义了宽度,以匹配 position_dodge中的 geom_point宽度。
enter image description here

关于r - 沿x轴按组划分geom_point点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46982172/

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