我想绘制一个点范围图,其中组的点不相互堆叠。该图应如下所示:
我最好的躲避尝试是在躲避参数中使用向量:
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
中的dodge参数? 最佳答案
从逻辑上考虑,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()
关于r - 如何在两个级别上避开pointrange ggplots?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15472158/