gpt4 book ai didi

r - 多变量图的 geom_segment 线垂直

转载 作者:行者123 更新时间:2023-12-03 23:38:22 29 4
gpt4 key购买 nike

下面的绘图代码使所有颜色的线段线会聚在离散 x 变量的中心。可以做些什么来使线段线对于每个 x 变量各自的颜色是垂直的?

#~ Plot data
ggplot(data = data0, aes(x = v3, y = v4)) +
geom_point(aes(colour = v2),
size = 2,
alpha = .25,
position = position_jitterdodge(jitter.width = 0.15, seed = 1)
) +
stat_summary(fun = mean, geom = "point", size = 5, aes(colour = v2), position = position_dodge(0.75)) +
geom_segment(data = data1, aes(x = v3, xend = v3, y = v4, yend = grand_mean, colour = v2),
size = 1, position = position_dodge(width = 0.75)) +
geom_hline(data = data1, aes(yintercept = grand_mean)) +
facet_wrap(facets = vars(v1)) +
theme_bw()
multivariable plot
数据:
#~ Raw data
data0 <- data.frame(v1 = sample(c("foo", "bar"), 1000, replace = TRUE),
v2 = sample(letters[1:4], 1000 , replace = TRUE),
v3 = sample(1:4, 1000, replace = TRUE),
v4 = rnorm(1000))

#~ Summary data
data1 <- data0 %>%
group_by(v1, v2, v3) %>%
summarise(v4 = mean(v4))
data1$grand_mean <- mean(data0$v4)

最佳答案

position_dodge()无法处理 xendyend .使用 geom_line()geom_linerange()反而:

ggplot(data = data0, aes(x = v3, y = v4)) +
geom_point(aes(colour = v2),
size = 2,
alpha = .25,
position = position_jitterdodge(jitter.width = 0.15, seed = 1)
) +
stat_summary(fun = mean, geom = "point", size = 5, aes(colour = v2), position = position_dodge(0.75)) +
geom_linerange(data = data1, aes(x = v3, ymin = v4, ymax = grand_mean, colour = v2),
size = 1, position = position_dodge(width = 0.75)) +
geom_hline(data = data1, aes(yintercept = grand_mean)) +
facet_wrap(facets = vars(v1)) +
theme_bw()

关于r - 多变量图的 geom_segment 线垂直,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67869849/

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