在ggplot2中没有scale_y_continuous()的中断

时间:2013-03-19 08:18:58

标签: r ggplot2

我正在寻找一种方法,在不使用scale_y _...(breaks = c(x1,x2))函数的情况下在绘图中设置中断。问题如下:我想要一些箱图。

    require(ggplot2)
    a <- rnorm(10, 0, 5)
    a <- as.data.frame(a); colnames(a) <- "test"

    ### original boxplot
    ggplot(data=a, mapping=aes(y=test, x=rep(1,10))) +
      geom_boxplot()

    ### scale_y_continous() cuts of my data points and changes the boxplot!
    ggplot(data=a, mapping=aes(y=test, x=rep(1,10))) +
      geom_boxplot() +
      scale_y_continuous(limits=c(-1,1), breaks=c(-1,0,1))

    ### I am therefore using coord_cartesian() but I am missing a breaks() function
    ggplot(data=a, mapping=aes(y=test, x=rep(1,10))) +
      geom_boxplot() +
      coord_cartesian(ylim = c(-1,1)) # +
    # breaks(c(-1,0,1))   # something like this

感谢您的帮助!

1 个答案:

答案 0 :(得分:14)

您可以在一个图中合并coord_cartesian()scale_y_continuous(),只需从缩放功能中删除limits=c(-1,1)即可。在缩放函数中使用limits=时,数据会在该diapason中进行子集化。 coord_cartesian()只是缩放该值区域。

 ggplot(data=a, mapping=aes(y=test, x=rep(1,10))) +
      geom_boxplot() +
      coord_cartesian(ylim = c(-1,1))+
      scale_y_continuous(breaks=c(-1,0,1))