在ggplot2中结合点图,geom_segment和线条

时间:2018-07-31 13:28:36

标签: r ggplot2 grouping line boxplot

我反复测量了雄性和雌性动物在四种不同生殖状态(原始,交配,预期和亲代)下的既定行为。我想以以下方式表示我的数据(x:生殖状态,y:行为值):

  1. 点图,具有相同行为值的点水平分布而不是重叠
  2. 每个子组(例如处女男性)也应有一个细分,以显示行为的平均值
  3. 每只动物还应易于处理,并用细线连接各个繁殖状态下与该动物相对应的点

我设法完成了1)和2),但是无法将它们与我的3)目标结合起来。有人可以帮我吗?

这里是一个例子:

library(ggplot2)

Function to obtain a mean segment for each group
MinMeanSEMMax <- function(x) {
  v <- c(min(x), mean(x) - sd(x)/sqrt(length(x)), mean(x), mean(x) + sd(x)/sqrt(length(x)), max(x))
  names(v) <- c("ymin", "lower", "middle", "upper", "ymax")
  v
}

# Mock dataframe:
Sex<-rep(c("M","F"), times=12)
ID<-rep(seq(from=1, to=6), times=4)
Behavior<-rnorm(24, mean=10, sd=3)
State<-rep(c("virgin", "virgin", "mated", "mated", "expecting", "expecting", "parent", "parent"), times=3)
d<-data.frame(ID,Sex,Behavior,State)

# Prepare mean value for plotting of mean segments
  g<-ggplot(d, aes(x=factor(State), y=Behavior, colour=Sex))+
    stat_summary(fun.data=MinMeanSEMMax, geom="boxplot", position=position_dodge(), outlier.shape = 21, outlier.size = 3, size=1)+
    scale_x_discrete(limits=c("virgin", "mated", "expecting", "parent"), labels=c("virgin"="Virgin", "mated"="Mated", "expecting"="Expecting", "parent"="Parent"))+
  dat.g <- ggplot_build(g)$d[[1]]
  g

  # The plot

  b<-ggplot(d, aes(x=factor(State), y=Behavior, colour=factor(Sex)))+
    geom_segment(data=dat.g, aes(x=xmin, xend=xmax,y=middle, yend=middle), colour=c("blue3","brown2","blue3","brown2","blue3","brown2","blue3","brown2"), size=1)+
    geom_dotplot(aes(fill=Sex),binaxis="y", stackdir="center", position=position_dodge(width=1), binwidth = 0.3)+
    labs(x="",y="Behavior")+
    theme_classic()+ 
    theme(axis.line.x = element_line(color="black", size = 1),
          axis.line.y = element_line(color="black", size = 1))+
    theme(legend.position="none")+
    theme(axis.text.x =element_text(size=10),axis.text.y=element_text(size=10), axis.title=element_text(size=11,face="bold"))+
    scale_fill_manual(name="Sex", values=c("brown2", "blue3"), breaks=c("F", "M"))+
    scale_colour_manual(name="Sex",values=c("brown2","blue3"),breaks=c("F", "M"),labels=c("Female", "Male"))+
    scale_x_discrete(limits=c("virgin", "mated", "expecting", "parent"), labels=c("virgin"="Virgin", "mated"="Mated", "expecting"="Expecting", "parent"="Parent"))+
    theme(text=element_text(family="serif"))
  b

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以尝试

library(tidyverse)
d %>% 
  mutate(State=factor(str_to_title(State), levels = c("Virgin", "Mated", "Expecting", "Parent"))) %>% 
  mutate(State2=ifelse(Sex=="F", as.numeric(State) + 0.25, as.numeric(State) - 0.25)) %>% 
  ggplot(aes(x=State, y=Behavior, fill=Sex)) + 
   geom_blank()+
   geom_dotplot(aes(x=State2,group=interaction(State,Sex)), binaxis="y", stackdir="center", binwidth = 0.3)+
   stat_summary(aes(x=State2),fun.y = "mean", geom="point",
               shape=95,size=8, show.legend = F) +
   geom_line(aes(x=State2, group=interaction(ID, Sex), color= Sex), alpha=0.5)

enter image description here

这个想法类似于答案here。自己进行躲避以使线条正确。此外,您还必须使用interaction来获得点和线的正确位置。