gpt4 book ai didi

r - 如何在两个级别上躲避pointrange ggplots?

转载 作者:行者123 更新时间:2023-12-03 20:54:19 24 4
gpt4 key购买 nike

我想制作一个点范围图,其中组的点不相互堆叠。情节应该是这样的:enter image description here :

我最好的躲避尝试是在躲避参数中使用向量:

library(ggplot2)

dat <- structure(list(Treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"),
Temp = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L), .Label = c("10", "20"), class = "factor"), Rep = c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), Meas = c(3L,
2L, 2L, 2L, 6L, 4L, 4L, 3L, 5L, 1L, 2L, 3L), SD = c(2L, 3L,
2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 1L)), .Names = c("Treatment",
"Temp", "Rep", "Meas", "SD"), row.names = c(NA, -12L), class = "data.frame")

ggplot(dat, aes(x = Treatment, y = Meas, ymin = Meas - SD/2, ymax = Meas + SD/2)) +
geom_linerange(aes(color = Temp), position=position_dodge(width=c(0.6,0.4)), size = 1, alpha = 0.5) +
geom_point(aes(color = Temp, shape = Temp), position=position_dodge(width=c(0.6,0.4)), size = 3) +
theme_bw()

这导致了如下所示的情节。然而,所有的点都没有被躲避,我必须在 Illustrator 中移动点和误差线才能得到上面的绘图显示。 有没有办法在 ggplot2 中使用闪避参数?两个层面?
enter image description here

最佳答案

逻辑思考,position_dodge更适合酒吧。它确实适用于 lineranges在一个因素级别上,但是在第二个级别上,很难定义线之间的最小距离。虽然您可以对因素进行数字区分,然后为标签添加适当的位置。

dat1<-cbind(dat,aux=rep(1,length(dat[,1]))) 
dat1<-within(dat1, {aux = unlist(by(aux,Treatment,cumsum))})
dat1$aux<-dat1$aux+as.numeric(dat1$Treatment)*10
ggplot(dat1, aes( x=aux, y = Meas, ymin = Meas - SD/2, ymax = Meas + SD/2)) +
geom_linerange(aes(color = Temp), size = 1, alpha = 0.5) +geom_point(aes(color = Temp, shape = Temp))+
scale_x_continuous("Treatment",breaks=c(13.5,23.5), labels=c("A","B")) + # here you define coordinates for A and B
theme_bw()

enter image description here

关于r - 如何在两个级别上躲避pointrange ggplots?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15472158/

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