我想为使用 facet_wrap 函数创建的每个箱线图添加一条水平线。这是我创建箱线图的代码:
ggplot(data=new.dat, aes(variable, value)) + geom_boxplot() +
facet_wrap(~variable, scales="free", ncol=4) +
scale_x_discrete(breaks=NULL) +
labs(x="",y="")
这是我想用于每条水平线的数据框:
dat_hlines <- data.frame(cond=c("S1mgg","S2mgg","S3mgg","TOC",
"HI","OI","PI","TmaxC"),
hline=c(7.5,20,7.5,400,10,10,400,500))
这是我最接近的:
ggplot(data=new.dat, aes(variable, value)) + geom_boxplot() +
geom_hline(data=dat_hlines,aes(yintercept=hline)) +
facet_wrap(~variable, scales="free", ncol=4) +
scale_x_discrete(breaks=NULL) +
labs(x="",y="")
出现的是多行,即在每个箱线图上重复的行。
我想要的是每幅图一条线,每条线对应于 dat_hlines$hline 中的观察结果
ps 我会发布图形,但我的分数不允许。
数据
tmp<- data.frame(matrix(vector(), 200, 2, dimnames=list(c(),
c("variable", "value"))), stringsAsFactors=F)
tmp[1]<-"S1mgg"
tmp[2]<-rnorm(200, mean=10, sd=3)
tmp2<- data.frame(matrix(vector(), 200, 2, dimnames=list(c(),
c("variable", "value"))), stringsAsFactors=F)
tmp2[1]<-"S2mgg"
tmp2[2]<-rnorm(200, mean=10, sd=3)
tmp3<- data.frame(matrix(vector(), 200, 2, dimnames=list(c(),
c("variable", "value"))), stringsAsFactors=F)
tmp3[1]<-"S3mgg"
tmp3[2]<-rnorm(200, mean=10, sd=3)
tmp4<- data.frame(matrix(vector(), 200, 2, dimnames=list(c(),
c("variable", "value"))), stringsAsFactors=F)
tmp4[1]<-"S3mgg"
tmp4[2]<-rnorm(200, mean=10, sd=3)
tmp5<- data.frame(matrix(vector(), 200, 2, dimnames=list(c(),
c("variable", "value"))), stringsAsFactors=F)
tmp5[1]<-"TOC"
tmp5[2]<-rnorm(200, mean=10, sd=3)
tmp6<- data.frame(matrix(vector(), 200, 2, dimnames=list(c(),
c("variable", "value"))), stringsAsFactors=F)
tmp6[1]<-"HI"
tmp6[2]<-rnorm(200, mean=10, sd=3)
tmp7<- data.frame(matrix(vector(), 200, 2, dimnames=list(c(),
c("variable", "value"))), stringsAsFactors=F)
tmp7[1]<-"OI"
tmp7[2]<-rnorm(200, mean=10, sd=3)
tmp8<- data.frame(matrix(vector(), 200, 2, dimnames=list(c(),
c("variable", "value"))), stringsAsFactors=F)
tmp8[1]<-"TmaxC"
tmp8[2]<-rnorm(200, mean=10, sd=3)
new.dat<-rbind(tmp,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7,tmp8)