我测量了各种生殖状态(从处女到父母)的动物(雄性和雌性)的特定行为。
我正在绘制一个带有躲闪点的点图,我想为每个子组添加一个中间条。我之前问了一个类似的问题,我被告知使用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
语句中使用stat_summary
,就像这样:stat_summary(aes(x=State2),fun.y = "median", fun.ymax="median", fun.ymin="median", geom="crossbar", show.legend = F) +
如何轻松添加准确的中位数条?谢谢
尝试使用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