How to vary binwidth inside ggplot?

60 views
Skip to first unread message

Sav Tan

unread,
Nov 8, 2011, 9:33:19 AM11/8/11
to ggplot2
Hello,

I'am doing histogram with ggplot.

p <- ggplot(TotCalc, aes(x=x,y=100*(..count../sum(..count..)))) +
xlab(xlabel) + ylab(ylabel) +
geom_histogram(colour = "darkblue", fill = "white", binwidth=500)
my x is between 2 and 6580 and I have 2600 data.

Like this I have 10 bins, each one have a width 500 but

I want to plot one histogram with different binwidth for each bin. Is
it possible?

For example, I want to have 8 bins, with width like this:

c(180,100,110,160,200,250,1000,3000)
How can I do it?

Brandon Hurr

unread,
Nov 8, 2011, 9:37:56 AM11/8/11
to Sav Tan, ggplot2
Could you just save your vector of binwidths and use a for loop to iterate over the widths? 

You can save each plot in a list to call later or use ggsave() to save each image using paste() to input the name of your x and the binwidth. 

Sound reasonable?

B


--
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

Sav Tan

unread,
Nov 8, 2011, 9:50:25 AM11/8/11
to ggplot2
Maybe I can do what you said
But also I can just use breaks, like this
ggplot(TotCalc, aes(x=pCO2,y=100*(..count../sum(..count..)))) +
xlab(xlabel) + ylab(ylabel) +
geom_histogram(colour = "darkblue", fill = "white",
breaks=cumsum(c(180,100,110,160,200,250,1000,3000)), position="dodge")

Brandon Hurr

unread,
Nov 8, 2011, 9:54:17 AM11/8/11
to Sav Tan, ggplot2
I think I misunderstood what you were after... Does giving the breaks a vector solve your problem? 

Sav Tan

unread,
Nov 8, 2011, 11:02:04 AM11/8/11
to ggplot2
yes
Reply all
Reply to author
Forward
0 new messages