먼저 샘플 데이터가 제공되지 않으므로 내 샘플 데이터를 만들었습니다. 이러한 데이터는 이미 수준의 각 조합 당 하나의 값이 (요약되어있다.
이
set.seed(1)
df<-data.frame(expand.grid(c("Control","Effect"),c("Self","Other"),c("Type1","Type2")),
runif(8,0,1))
colnames(df)<-c("Treatment","Group","Type","value")
df
Treatment Group Type value
1 Control Self Type1 0.2655087
2 Effect Self Type1 0.3721239
3 Control Other Type1 0.5728534
4 Effect Other Type1 0.9082078
5 Control Self Type2 0.2016819
6 Effect Self Type2 0.8983897
7 Control Other Type2 0.9446753
8 Effect Other Type2 0.6607978
지금 당신이 라인의 위치에 대한 두 개의 새로운 값을 추가해야합니다. ymin
값이 원래의 값을 더한 작은 상수이다. ymax
값을 (Treatment
및 그룹화 등 Type
를 사용하여) 각각의면에 대해 계산하고 최대 패싯 값 더하기 일부 상수이다.
library(plyr)
df<-ddply(df,.(Treatment,Type),transform,ymax=max(value)+0.2)
df$ymin<-df$value+0.05
df
Treatment Group Type value ymax ymin
1 Control Self Type1 0.2655087 0.7728534 0.3155087
2 Control Self Type2 0.2016819 1.1446753 0.2516819
3 Control Other Type1 0.5728534 0.7728534 0.6228534
4 Control Other Type2 0.9446753 1.1446753 0.9946753
5 Effect Self Type1 0.3721239 1.1082078 0.4221239
6 Effect Self Type2 0.8983897 1.0983897 0.9483897
7 Effect Other Type1 0.9082078 1.1082078 0.9582078
8 Effect Other Type2 0.6607978 1.0983897 0.7107978
제 2 데이터 프레임이 레이블된다. 여기서 각 패싯 y 위치에하면 다시 원래이다 ymax
값 + 상수 및 lab
에 표시해야하는 레이블이 있습니다.
df.names<-ddply(df,.(Treatment,Type),summarise,ymax=ymax[1]+0.1)
df.names$lab<-c("p=0.46","**","***","*")
df.names
Treatment Type ymax lab
1 Control Type1 0.8728534 p=0.46
2 Control Type2 1.2446753 **
3 Effect Type1 1.2082078 ***
4 Effect Type2 1.1983897 *
를 지금 df
이미 요약 값 대신 사용 stat_summary()
의 geom_bar(stat="identity")
. 추가 행은 두 개의 geom_segment()
호출과 함께 추가됩니다. 첫 번째 행은 세로선을 표시하고 두 번째 행은 가로선을 추가합니다. geom_text()
은 선 위에 레이블을 추가합니다. 양해, 디드에 대한
ggplot(df, aes(Group,value,fill=Group)) +
geom_bar(stat="identity") + facet_grid(Type~Treatment) +
theme(legend.position="none")+
geom_segment(aes(x=Group,xend=Group,y=ymin,yend=ymax))+
geom_segment(aes(x="Self",xend="Other",y=ymax,yend=ymax))+
geom_text(data=df.names,aes(x=1.5,y=ymax,label=lab),inherit.aes=FALSE)

매우 감사합니다. 그것은 나를 위해 매우 분명하고 완전합니다. –