在ggplot2中没有scale_y_continuous()的中断

Dan*_*oop 4 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
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

Did*_*rts 14

您可以结合coord_cartesian()scale_y_continuous()在一个情节,只是删除limits=c(-1,1)从规模功能.在limits=scale函数内部使用时,数据在该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))
Run Code Online (Sandbox Code Playgroud)