Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

breaks without scale_y_continuous() in ggplot2

Tags:

r

ggplot2

I was searching for a method to set breaks in a plot without using the scale_y_...(breaks=c(x1,x2)) function. The problem is the following: I want some boxplots.

    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

Thank you for your help!

like image 929
Daniel Hoop Avatar asked Mar 19 '13 08:03

Daniel Hoop


1 Answers

You can combine coord_cartesian() and scale_y_continuous() in one plot, just remove limits=c(-1,1) from scale function. When you use limits= inside the scale function, data are subsetted in that diapason. coord_cartesian() just zooms that region of values.

 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))
like image 59
Didzis Elferts Avatar answered Sep 20 '22 01:09

Didzis Elferts