Possible to centre x-axis ticks in geom_histogram?

4,188 views
Skip to first unread message

scott smith

unread,
Nov 16, 2011, 6:42:58 AM11/16/11
to ggplot2
Hi,

just wondering if it is possible to move the x-axis ticks when using
geom_histogram so that they are in the middle of the bars, rather
than
the right hand edge of the bar.

can't find any mention on google....

here's my simple 2line plot

> m<-ggplot(data,aes(x=rest_delta))
> m + geom_histogram(aes(fill=..count..),binwidth=1)

thanks,
Scott


Brandon Hurr

unread,
Nov 16, 2011, 8:18:31 AM11/16/11
to scott smith, ggplot2
If you got rid of the grey grid using theme_bw() and then the tick marks you could adjust the position of the axis text using opts() and vjust/hjust to get them where you want. 

That's a pretty manual solution though... perhaps there is a more automated solution? 



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Hadley Wickham

unread,
Nov 16, 2011, 10:47:25 AM11/16/11
to Brandon Hurr, scott smith, ggplot2
There's no general solution, because tick marks are supposed to
reflect the data scale...

But may you want to adjust the origin of the histogram so it falls
between the ticks:

library(ggplot2)
df <- data.frame(x = runif(100))
qplot(x, data = df, binwidth = 0.1)
qplot(x, data = df, binwidth = 0.1, origin = -0.05)

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

scott smith

unread,
Nov 16, 2011, 11:35:58 AM11/16/11
to ggplot2
Thanks guys - the 'origin' command does the trick nicely!

cheers
Scott

On Nov 16, 3:47 pm, Hadley Wickham <had...@rice.edu> wrote:
> There's no general solution, because tick marks are supposed to
> reflect the data scale...
>
> But may you want to adjust the origin of the histogram so it falls
> between the ticks:
>
> library(ggplot2)
> df <- data.frame(x = runif(100))
> qplot(x, data = df, binwidth = 0.1)
> qplot(x, data = df, binwidth = 0.1, origin = -0.05)
>
> Hadley
>
>
>
> On Wed, Nov 16, 2011 at 7:18 AM, Brandon Hurr <brandon.h...@gmail.com> wrote:
> > If you got rid of the grey grid using theme_bw() and then the tick marks you
> > could adjust the position of the axis text using opts() and vjust/hjust to
> > get them where you want.
> > That's a pretty manual solution though... perhaps there is a more automated
> > solution?
>

Gloria Kreitinger

unread,
May 24, 2013, 1:53:32 PM5/24/13
to ggp...@googlegroups.com
Is there an analogous solution to shifting the x-axis when creating a ggplot object (not a qplot object)?

Thank you,

Gloria

Ito, Kaori (Groton)

unread,
May 24, 2013, 1:58:04 PM5/24/13
to Gloria Kreitinger, ggp...@googlegroups.com

qplot(x, data = df, binwidth = 0.1, origin = -0.05)

 

is same with:

 

ggplot(data=df, aes(x=x)) + geom_histogram(binwidth = 0.1, origin = -0.05)

--

--
You received this message because you are subscribed to the ggplot2 mailing list.

Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility


 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gloria Kreitinger

unread,
May 24, 2013, 2:11:17 PM5/24/13
to ggp...@googlegroups.com, Gloria Kreitinger
Thank you!
Reply all
Reply to author
Forward
0 new messages