使用ggplot2仅将一个段添加到一个方面 [英] Add a segment only to one facet using ggplot2

查看:204
本文介绍了使用ggplot2仅将一个段添加到一个方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举个例子,我有这个数据框,叫做 my_data

 组因子A因子B因子C N值sd se ci 
1控制条件1条件1条件1 3 92.00000 6.0827625 3.511885 15.110420
2控制条件1条件1条件2 2 69.00000 8.4852814 6.000000 76.237228
3控制条件1条件2条件1 3 72.33333 10.2632029 5.925463 25.495209
4控制条件1条件2条件2 2 97.00000 2.8284271 2.000000 25.412409
5控制条件2条件1条件1 3 85.00000 13.0000000 7.505553 32.293790
6控制条件2条件1条件2 2 78.50000 16.2634560 11.500000 146.121354
7控制条件2条件2条件1 3 95.00000 5.1961524 3.000000 12.907958
8控制条件2条件2条件2 2 78.00000 22.6274170 16.000000 203.299276
9实验条件1 Co ndition1 Condition1 2 80.00000 5.6568542 4.000000 50.824819
10实验条件1条件1条件2 3 74.00000 19.9248588 11.503623 49.496093
11实验条件1条件2条件1 2 68.50000 0.7071068 0.500000 6.353102
12实验条件1条件2条件2 3 78.66667 18.5831465 10.728985 46.163095
13实验条件2条件1条件1 2 81.00000 19.7989899 14.000000 177.886866
14实验条件2条件1条件2 3 75.33333 17.0391706 9.837570 42.327646
15实验条件2条件2条件1 2 81.50000 14.8492424 10.500000 133.415150
16实验条件2条件2条件2 3 78.00000 5.2915026 3.055050 13.144821

使用此代码创建

  my_data<  -  data.frame(Groups = c(rep(Control,20),rep(Experimental,20)),
FactorA = rep (c(条件1,条件2),20),
FactorB = rep(c(条件1,条件1,条件2,条件2),10),
因子C = ,4)),5),
value = sample(60:100,40,replace = T)


#添加特定函数的标准错误
my_data< - summarySE(my_data,
measurevar =value,
groupvars = c(Groups,FactorA,FactorB,FactorC))

然后,用以下代码:

  ggplot(my_data,aes(Groups,value,fill = FactorA))+ 
geom_bar(position = position_dodge(),stat =identity)+
geom_errorbar(aes(ymin = value- se,ymax = value + se),
width = .1,#误差线宽度
position = position_dodge(.9))+
facet_grid(FactorB〜FactorC)+
geom_segment(x = 0.8,y = 100,xend = 1.2,yend = 100)+
主题(axis.text.x = element_text(size = 12,colo ur =black),
axis.title.x = element_blank(),
legend.key.size = unit(1,cm),
legend.text = element_text(大小= 14),
plot.title = element_text(lineheight = .8,face =bold),
panel.margin =单位(1,lines),
轴。 title.y = element_text(size = 16,vjust = 0.3),
strip.text.y = element_text(size = 16,face =bold),
strip.text.x = element_text( size = 16,face =bold))+
coord_cartesian(ylim = c(60,120))

我得到这张图



正如您所看到的,使用 geom_segment(x = 0.8,y = 100,xend = 1.2,yend = 100)这里出现了四个分段,每个分面一个,但我只想在一个分面上绘制一个分段(例如,在左上方的图中)。



有人可以帮我吗?我尝试了各种解决方案,但没有任何解决方案。 解决方案

code> FactorB FactorC ,您需要绘制细分的级别。

  data.segm <-data.frame(x = 0.8,y = 100,xend = 1.2,yend = 100,
FactorB =Condition1,FactorC = Condition1)

现在使用此数据框添加细分。同时在 geom_segment()中添加 inherit.aes = FALSE 来忽略ggplot()中设置的fill = FactorA。 p>

  + geom_segment(data = data.segm,
aes(x = x,y = y,yend = yend,xend = xend),inherit.aes = FALSE)+


As an example, I have this data frame, called my_data:

         Groups    FactorA    FactorB    FactorC N    value         sd        se         ci
1       Control Condition1 Condition1 Condition1 3 92.00000  6.0827625  3.511885  15.110420
2       Control Condition1 Condition1 Condition2 2 69.00000  8.4852814  6.000000  76.237228
3       Control Condition1 Condition2 Condition1 3 72.33333 10.2632029  5.925463  25.495209
4       Control Condition1 Condition2 Condition2 2 97.00000  2.8284271  2.000000  25.412409
5       Control Condition2 Condition1 Condition1 3 85.00000 13.0000000  7.505553  32.293790
6       Control Condition2 Condition1 Condition2 2 78.50000 16.2634560 11.500000 146.121354
7       Control Condition2 Condition2 Condition1 3 95.00000  5.1961524  3.000000  12.907958
8       Control Condition2 Condition2 Condition2 2 78.00000 22.6274170 16.000000 203.299276
9  Experimental Condition1 Condition1 Condition1 2 80.00000  5.6568542  4.000000  50.824819
10 Experimental Condition1 Condition1 Condition2 3 74.00000 19.9248588 11.503623  49.496093
11 Experimental Condition1 Condition2 Condition1 2 68.50000  0.7071068  0.500000   6.353102
12 Experimental Condition1 Condition2 Condition2 3 78.66667 18.5831465 10.728985  46.163095
13 Experimental Condition2 Condition1 Condition1 2 81.00000 19.7989899 14.000000 177.886866
14 Experimental Condition2 Condition1 Condition2 3 75.33333 17.0391706  9.837570  42.327646
15 Experimental Condition2 Condition2 Condition1 2 81.50000 14.8492424 10.500000 133.415150
16 Experimental Condition2 Condition2 Condition2 3 78.00000  5.2915026  3.055050  13.144821

created with this code

my_data <- data.frame(Groups=c(rep("Control",20),rep("Experimental",20)),
                      FactorA=rep(c("Condition1","Condition2"),20),
                      FactorB=rep(c("Condition1","Condition1","Condition2","Condition2"),10),
                      FactorC=rep(c(rep("Condition1",4),rep("Condition2",4)),5),
                      value=sample(60:100,40,replace=T)
                      )

# add standard errors with a specific function    
my_data <- summarySE(my_data,
                     measurevar="value",
                     groupvars=c("Groups","FactorA","FactorB", "FactorC"))

Then, with this code:

    ggplot(my_data, aes(Groups,value,fill=FactorA)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-se, ymax=value+se),
               width=.1,                    # Width of the error bars
               position=position_dodge(.9)) +
  facet_grid(FactorB~FactorC) +
  geom_segment(x=0.8,y=100,xend=1.2,yend=100) +
  theme(axis.text.x=element_text(size=12, colour="black"),
        axis.title.x=element_blank(),
        legend.key.size=unit(1,"cm"),
        legend.text=element_text(size=14),
        plot.title=element_text(lineheight=.8, face="bold"),
        panel.margin=unit(1, "lines"),
        axis.title.y = element_text(size = 16, vjust=0.3),
        strip.text.y = element_text(size=16,face="bold"),
        strip.text.x = element_text(size=16,face="bold")) +
  coord_cartesian(ylim=c(60, 120))

I obtain this plot

As you can see, with the line geom_segment(x=0.8,y=100,xend=1.2,yend=100) there appear four segments, one for each facet, but I want to draw a segment only in one facet (for example in the upper left plot).

Could someone help me, please? I tried various solutions but nothing.

解决方案

Put the data for the segment in data frame and also add columns FactorB and FactorC with levels for which you need to draw the segment.

data.segm<-data.frame(x=0.8,y=100,xend=1.2,yend=100,
                      FactorB="Condition1",FactorC="Condition1")

Now use this data frame to add segment. Also add inherit.aes=FALSE inside geom_segment() to ignore fill=FactorA set in ggplot().

  + geom_segment(data=data.segm,
               aes(x=x,y=y,yend=yend,xend=xend),inherit.aes=FALSE)+

这篇关于使用ggplot2仅将一个段添加到一个方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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