小编use*_*449的帖子

在 R 中使用 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="") 
Run Code Online (Sandbox Code Playgroud)

这是我想用于每条水平线的数据框:

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))
Run Code Online (Sandbox Code Playgroud)

这是我最接近的:

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="") 
Run Code Online (Sandbox Code Playgroud)

出现的是多条线,即在每个箱线图上重复的相同线。

我想要的是每个地块一条线,每条线与 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, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 facet-wrap

2
推荐指数
1
解决办法
3327
查看次数

标签 统计

facet-wrap ×1

ggplot2 ×1

r ×1