在R中使用镶嵌面_包裹时,将水平线(或文本)添加到各个箱线图
我想在使用facet_wrap函数创建的每个箱线图中添加一条水平线。这是创建箱线图的代码:在R中使用镶嵌面_包裹时,将水平线(或文本)添加到各个箱线图,r,ggplot2,facet-wrap,R,Ggplot2,Facet Wrap,我想在使用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="") 这是我希望用
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))
出现的是多条线,即在每个箱线图上重复相同的线
我想要的是每个绘图一行,每行对应于dat_hlines$hline中的观察值
p、 我本来会发布图片的,但我的分数不允许
资料
tmp这个怎么样
new_data <- merge(new.dat, dat_hlines, by.x = "variable", by.y = "cond")
ggplot(data=new_data, aes(variable, value)) + geom_boxplot() +
facet_wrap(~variable, scales="free", ncol=4) +
scale_x_discrete(breaks=NULL) +
labs(x="",y="") + geom_hline(aes(yintercept = hline))
new_data你能提供数据吗?是的,我能,最好的方法是什么?在哪里?如果你想得到帮助,你需要给出一个可复制的例子,请原谅粗制滥造的编码,但是……请参见上面编辑的文本。您将能够使用“new.dat”和“dat\u hlines”重现问题。如果您使用这种方法,则使用geom\u text
在需要时添加标签非常容易。只要确保您设置了检查重叠=真
,如果您这样做了。
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)
new_data <- merge(new.dat, dat_hlines, by.x = "variable", by.y = "cond")
ggplot(data=new_data, aes(variable, value)) + geom_boxplot() +
facet_wrap(~variable, scales="free", ncol=4) +
scale_x_discrete(breaks=NULL) +
labs(x="",y="") + geom_hline(aes(yintercept = hline))