在ggplot中为中位数_hilow避开垂直线

时间:2019-06-20 05:58:47

标签: r ggplot2

我需要绘制显示多个样本的3个重复的中值和IQR的线。

数据:

sampleid <- rep(1:20, each = 3)
replicate <- rep(1:3, 20)
sample1 <- seq(120,197, length.out =  60)
sample2 <- seq(113, 167, length.out = 60)
sample3 <- seq(90,180, length.out = 60)

到目前为止我做了什么?

df <- as.data.frame(cbind(sampleid,replicate,sample1, sample2, sample3))

library(reshape2) 

long <- melt(df,id.vars = c('sampleid', 'replicate')) 

ggplot(data = long, aes(x = variable, y = value, colour = factor(replicate)))   + stat_summary(fun.data=median_hilow, conf.int=.5)

但是,对于每个样本,我得到的重复样本的IQR曲线相互重叠。我想找到一种方法来“躲避”这3条线,以便它们彼此相邻可见,而无需更改已实现的绘图的其他参数。这可以实现吗?

1 个答案:

答案 0 :(得分:0)

您必须在行中插入jitter

ggplot(data = long, aes(x = variable, y = value, colour = factor(replicate))) +
  stat_summary(fun.data=median_hilow, fun.args = (conf.int=.5), position = "jitter")

请注意,您还需要将conf.int=5包裹在fun.args中。

或者,将x更改为factor(replicate)并添加facet_wrap

ggplot(data = long, aes(x = factor(replicate), y = value, colour = factor(replicate))) +
  stat_summary(fun.data=median_hilow, fun.args = (conf.int=.5)) +
  facet_wrap(~variable)