轴断裂/间隙与误差棒

时间:2014-08-01 07:13:29

标签: r plot

有很简单的方法可以制作轴断裂/间隙图。是否有一种类似的简单方法来制作带有误差条的轴断裂/间隙图? (我只知道这种方式)

# install.packages("plotrix", dependencies = TRUE)
require(plotrix)     

x <- c(1:20); y <- c(rnorm(10)+4, rnorm(10)+20)
gap.plot(x,y, gap=c(8,16), ytics=c(1:8,16:25))

enter image description here

errorbars <- function(x, y, err, width){
  x0 = x; y0 = (y-err)
  x1 = x; y1 = (y+err)
  arrows(x0, y0, x1, y1, code=3, angle=90, length=width)
}

err <- rep(1,20)
plot(x,y)
errorbars(x,y,err,0.05)

enter image description here

1 个答案:

答案 0 :(得分:0)

@Roland said

我会建议一个解决方案,你把它全部放在对数刻度上。像这样(见下面的代码)。左边是对数刻度上的那个,右边是标准线性刻度上的那个用于比较。

enter image description here

# install.packages("ggplot2", dependencies = TRUE)
require(ggplot2)
# data(mtcars) # add this if you rerun it to get a clean `mtcars`
mtcars$cyl2<- ifelse(mtcars$cyl > 4, c('A'), c('B')) 
# multiplying cylenders for B by 10
mtcars$mpg[mtcars$cyl2=='B'] <- mtcars$mpg[mtcars$cyl2=='B'] * 10
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p1 <- p + geom_boxplot() + facet_grid(. ~ cyl2, scales = "free", space = "free") + scale_y_log10() + labs(title = "On a logarithmic scale")
p2 <- p + geom_boxplot() + facet_grid(. ~ cyl2, scales = "free", space = "free") + labs(title = "On a standard linear scale")

# install.packages("gridExtra", dependencies = TRUE)
require(gridExtra)

grid.arrange(p1, p2, ncol=2)