如何在两个层面上躲避pointrange ggplots? [英] How to dodge pointrange ggplots on two levels?

查看:240
本文介绍了如何在两个层面上躲避pointrange ggplots?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个积分点图,其中积分点不会相互叠加。情节应该是这样的::



  library(ggplot2)$ b $ (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,3L,1L,2L,3L,1L, ,2L,3L,1L,2L,3L),Meas = c(3L,
2L,2L,2L,6L,4L,4L,3L,5L,1L,2L,3L) ,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_dod ge(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 两个级别中使用闪避参数吗?

解决方案

从逻辑上思考, 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 $治疗)* 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))+#在此定义A和B
的坐标theme_bw()


I would like to make a point range plot where points of groups are not stacked on each other. The plot should look like this::

My best attempt to do the dodging is to use a vector in dodge argument:

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()

Which leads to a plot shown below. However, all of the points are not dodged and I have to move the dots and error bars in Illustrator to get a plot show above. Is there a way to use dodge argument in ggplot2 on two levels?

解决方案

Thinking logically, position_dodge is more suitable for bars. It does work with lineranges on one factor level, however on the second level it's hard to define the minimum distance between the lines. Though you can make a numerical distinction between factors and then add proper position for labels.

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()

这篇关于如何在两个层面上躲避pointrange ggplots?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆