在ggplot2中使用scale =“free”指定刻面图的下限

时间:2018-05-14 15:05:01

标签: r ggplot2

df <- data.frame(Taxa=c(rep("A", 4), rep("B", 4)),
                 Day=as.factor(c(1,1,2,2,1,1,2,2)),
                 Treatment=c(rep(c("t1","t2"),4)),
                 Values=c(100,110,120,130, 120, 130,150, 1800),
                 SD = c(10,11,12,13,12,13,14,200)) # i have one big outlier in the data

library(ggplot2)
p1 <- ggplot(df, aes(x=Day, ymax=Values+SD, ymin=Values-SD, y=Values, fill=Treatment))+
  geom_bar(stat="identity", position=position_dodge(width=0.7), col="black", width=0.7) +
  facet_wrap(~Taxa, scale="free_y", nrow=2) +
  scale_y_continuous(labels = scales::scientific) +
  geom_errorbar(position=position_dodge(width=0.7), col="black", lty="solid", width=0.3)
p1

我试过了:

p1 + expand_limits(ylim = c(100, NA)) # is ignored
p1 + scale_y_continuous(labels = scales::scientific, limits = c(100, NA)) # removes geom_bar
p1 + coord_cartesian(ylim=100,NA)  # error

我被要求以固定的y值开始一个类似但更复杂的绘图,但是让ggplot分别计算每个面的x-max。但scale="free_y"似乎是不可能的。由于我有一个很大的异常值,我不想全局控制ymax。有没有办法实现这一目标,而无需单独绘制所有分类单元?

0 个答案:

没有答案