在ggplot2中的躲避点图上以适当的高度绘制中间线

时间:2019-04-03 15:53:18

标签: r ggplot2 median dot crossbar

我测量了各种生殖状态(从处女到父母)的动物(雄性和雌性)中的既定行为。

我正在绘制一个带有闪避点的点图,我想为每个子组添加一个中间条。我以前曾问过类似的问题,并被告知要在stat_summary中使用geom="point"# 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) d$State2=ifelse(d$Sex=="F", as.numeric(d$State) + 0.15, as.numeric(d$State) - 0.15) # horizontally dodging males to the left, females to the right # The plot b<-ggplot(d, aes(x=factor(State), y=Behavior, colour=factor(Sex)))+ geom_dotplot(aes(x=State2, group=interaction(State, Sex), fill=Sex),binaxis="y", stackdir="center", stackratio=1.5, binwidth = 0.3, binpositions="all", dotsize=1)+ stat_summary(aes(x=State2),fun.y = "median", geom="point", shape=45, size=20, show.legend = F, alpha=0.6) + labs(x="",y="Behavior")+ geom_line(aes(x=State2, group=interaction(ID, Sex), color=Sex), alpha=0.5, size=0.2)+ 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 来添加中间条(请参见下面的示例代码)。

我的问题是,该条似乎不规则地放置在实际中值附近,而不是准确地位于中值上(请参见下图)

geom=crossbar

example plot with added lines and medians

编辑: 我查找了其他可能性,例如在stat_summary语句中使用stat_summary(aes(x=State2),fun.y = "median", fun.ymax="median", fun.ymin="median", geom="crossbar", show.legend = F) +,例如: Please enter 'n' if you are a new user, 'e' if you are an existing user, or 's' to display scores, or 'f' if you forgot your password:

但是问题是相同的,这些条没有正确放置...: example with crossbar

如何轻松添加准确的中线? 谢谢

1 个答案:

答案 0 :(得分:0)

尝试使用geom_point代替geom_dotplot

p<-ggplot(d, aes(x=factor(State), y=Behavior, colour=factor(Sex)))+

  geom_point(aes(x=State2, group=interaction(State, Sex), fill=Sex), size = 5)+

  stat_summary(aes(x=State2),fun.y = "median", geom="point", shape=45, size=20, show.legend = F, alpha=0.6) +
  labs(x="",y="Behavior")+
  geom_line(aes(x=State2, group=interaction(ID, Sex), color=Sex), alpha=0.5, size=0.2)+
  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"))

顺便说一句,我认为您的数据和x标签不匹配,因为State数据框中的d列是factor变量,按字母顺序排列。您将需要重新排序levels = c("virgin", "mated", "expecting", "parent")

的级别

因此,它将类似于:

d<-data.frame(ID,Sex,Behavior,State)

# Reorder State levels
d$State <- factor(d$State, levels = c("virgin", "mated", "expecting", "parent"))

d$State2=ifelse(d$Sex=="F", as.numeric(d$State) + 0.15, as.numeric(d$State) - 0.15) # horizontally dodging males to the left, females to the right