limiting histogram x-axis

43 views
Skip to first unread message

Mike Lawrence

unread,
Mar 26, 2009, 12:49:51 PM3/26/09
to ggp...@googlegroups.com
I'm attempting to plot several plots with the same x-axis limits, one
of which is a histogram. However, if I set the histogram's limits to
range(observations) it excludes the first and last bin:

a=data.frame(obs=rnorm(10))
p=ggplot(
data = a
,aes(
x = obs
)
)+geom_histogram(
)
p
p+scale_x_continuous(
limits = range(a$obs)
)

One fix is to explicitly set a binwidth then modify the limits accordingly:

my_binwidth = 1
p=ggplot(
data = a
,aes(
x = obs
)
)+geom_histogram(
bindwidth=my_binwidth
)

p+scale_x_continuous(
limits = c(min(a$obs)-binwidth,max(a$obs)+binwidth)
)

But this seems rather inelegant. Any suggestions?


--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tinyurl.com/mikes-public-calendar

~ Certainty is folly... I think. ~

Jason Rupert

unread,
Mar 26, 2009, 12:59:05 PM3/26/09
to ggp...@googlegroups.com, Mike Lawrence

Inelegant is fine as long as it works:)

Do you have any advice for doing something similar for facetized models?

Specifically, changing the bin range for the northern axis shown in the plot produced by the following code?

For some reason, I can not come up with a way of extending the range of the Northern axis (top). I would like to add another empty bin from 80-100.

Thanks.


VADeaths_flat_df = stack(as.data.frame(VADeaths))
names(VADeaths_flat_df) = c('Data','Person')
ii<-1


counts <- ddply(VADeaths_flat_df, .(cut(Data, breaks=fullseq(range(Data), 20)), Person), nrow)
names(counts) <- c("bin", "person", "n")
#print(
qplot(person, n, data = counts, fill = person, geom="bar", stat="identity", width = 0.9) +
facet_grid(. ~ bin) + opts(axis.text.x = NULL) + opts(axis.text.y = theme_text(colour = "red")) +
opts(strip.text.x = theme_text(colour = "green", size=14))


--- On Thu, 3/26/09, Mike Lawrence <Mike.L...@dal.ca> wrote:

hadley wickham

unread,
Mar 26, 2009, 1:26:46 PM3/26/09
to jasonk...@yahoo.com, ggp...@googlegroups.com, Mike Lawrence
Hi Jason,

Oh, you want to add an extra empty panel? Hmm, I don't think that you
can do that in the current version, but I've made some changes so you
should be able to in the development version.

Hadley
--
http://had.co.nz/

Jason Rupert

unread,
Mar 30, 2009, 10:39:49 PM3/30/09
to hadley wickham, ggp...@googlegroups.com, Mike Lawrence

Hadley,

Okay - I finally got the 0.8.3 version of ggplot2 and plyr 0.1.6 development version to work for my ggplot2 example.

By any chance can you provide an example or advice that allows an "extra empty panel" to be added to the example below or something similar?

Thank you again for all your help.

Example:

In order to keep the scales on plots for multiple data sets it appears that I need to add an empty bin in the bar plot.

I tried to do this in the example below by expanding the range, i.e. range(c(Data, max_range)) where max_range is the desired max x-scale, however apparently this had no effect. I hoped it would have added an "empty" bin from 80-100.

Thanks for your help.


VADeaths_flat_df = stack(as.data.frame(VADeaths))
names(VADeaths_flat_df) = c('Data','Person')

max_range<-100
counts <- ddply(VADeaths_flat_df, .(cut(Data, breaks=fullseq(range(c(Data, max_range)), 20)), Person), nrow)
names(counts) <- c("bin", "person", "n")
print(qplot(person, n, data = counts, fill = person, geom="bar", stat="identity", width = 0.9) + facet_grid(. ~ bin) + opts(axis.text.x = NULL) + opts(axis.text.y = theme_text(colour = "red")) + opts(strip.text.x = theme_text(colour = "green", size=14)))



--- On Thu, 3/26/09, hadley wickham <h.wi...@gmail.com> wrote:

hadley wickham

unread,
Mar 30, 2009, 10:47:47 PM3/30/09
to jasonk...@yahoo.com, ggp...@googlegroups.com, Mike Lawrence
On Mon, Mar 30, 2009 at 9:39 PM, Jason Rupert <jasonk...@yahoo.com> wrote:
>
>
> Hadley,
>
> Okay - I finally got the 0.8.3 version of ggplot2 and plyr 0.1.6 development version to work for my ggplot2 example.
>
> By any chance can you provide an example or advice that allows an "extra empty panel" to be added to the example below or something similar?
>
> Thank you again for all your help.
>
> Example:
>
> In order to keep the scales on plots for multiple data sets it appears that I need to add an empty bin in the bar plot.
>
> I tried to do this in the example below by expanding the range, i.e. range(c(Data, max_range)) where max_range is the desired max x-scale, however apparently this had no effect.  I hoped it would have added an "empty" bin from 80-100.
>
> Thanks for your help.
>
>
> VADeaths_flat_df = stack(as.data.frame(VADeaths))
> names(VADeaths_flat_df) = c('Data','Person')
>
> max_range<-100
> counts <- ddply(VADeaths_flat_df, .(cut(Data, breaks=fullseq(range(c(Data, max_range)), 20)), Person), nrow)
> names(counts) <- c("bin", "person", "n")
> print(qplot(person, n, data = counts, fill = person, geom="bar", stat="identity", width = 0.9) +  facet_grid(. ~ bin) + opts(axis.text.x = NULL) + opts(axis.text.y = theme_text(colour = "red")) + opts(strip.text.x = theme_text(colour = "green", size=14)))
>

Instead of using facet_grid, use facet_wrap(~ bin, drop = FALSE, nrow = 1)

Hadley

--
http://had.co.nz/

Jason Rupert

unread,
Mar 31, 2009, 8:01:37 AM3/31/09
to hadley wickham, ggp...@googlegroups.com, Mike Lawrence

Awesome! Looks like that worked on the Mac. Will try it this morning on the PC. Thank you again for all your help.

Here is the link to the output:
http://n2.nabble.com/extra-empty-panel-example-in-ggplot2-0.8.3-td2562582.html

Here is the code used to produce it:

setwd("/RStuff/ggplot2buildstuff/plyr/")
source("load.r")
setwd("/RStuff/ggplot2buildstuff/ggplot2/")
source("load.r")
Warning message:
package 'ggplot2' was built under R version 2.8.1

VADeaths_flat_df = stack(as.data.frame(VADeaths))
names(VADeaths_flat_df) = c('Data','Person')

max_range<-100
counts <- ddply(VADeaths_flat_df, .(cut(Data, breaks=fullseq(range(c(Data, max_range)), 20)), Person), nrow)
names(counts) <- c("bin", "person", "n")

print(qplot(person, n, data = counts, fill = person, geom="bar", stat="identity", width = 0.9) + facet_wrap(~ bin, drop = FALSE, nrow = 1) + opts(axis.text.x = NULL) + opts(axis.text.y = theme_text(colour = "red")) + opts(strip.text.x = theme_text(colour = "green", size=14)))
Theme element axis.text.x missing
Theme element axis.text.x missing
Theme element axis.text.x missing
Theme element axis.text.x missing
Theme element axis.text.x missing


--- On Mon, 3/30/09, hadley wickham <h.wi...@gmail.com> wrote:

> From: hadley wickham <h.wi...@gmail.com>
> Subject: Re: extra empty panel example in ggplot2 0.8.3
> To: jasonk...@yahoo.com
> Cc: "ggp...@googlegroups.com" <ggp...@googlegroups.com>, "Mike Lawrence" <Mike.L...@dal.ca>

Jason Rupert

unread,
Mar 31, 2009, 9:45:16 AM3/31/09
to hadley wickham, ggp...@googlegroups.com, Mike Lawrence

Hadley, (in the below I am using ggplot2_0.8.3.tar.gz & plyr_0.1.6.tar.gz)

I may have spoke too soon. I tried out my second example, and there appears to be an issue with the "facet_wrap" when there is a string invovled in the facet (hopefully I am using the proper terminology).

Below is the example, but here is the error received:
"Error in `[.data.frame`(plot$data, , setdiff(cond, names(df)), drop = FALSE) : undefined columns selected"

Note, the example worked fine with "facet_grid", but switching over to "facet_wrap" seemed to cause an issue.

Any help is greatly appreciated.

Jason

# Note the ability to switch out the facet_grid and facet_wrap below:

Me_df<-data.frame(Data = c(1:15), Person = "Me")
You_df<-data.frame(Data = c(10:20), Person = "You")
Them_df<-data.frame(Data = c(15:25), Person = "Them")

Group_df_tmp<-rbind(Me_df,You_df)
Group_df<-rbind(Group_df_tmp,Them_df)

Group_df$Person <- factor(Group_df$Person, levels = c("Them", "You", "Me"))
#max_range<-100
#counts <- ddply(VADeaths_flat_df, .(cut(Data, breaks=fullseq(range(c(Data, max_range)), 20)), Person), nrow)

max_range<-30
#counts <- ddply(Group_df, .(cut(Data, breaks=fullseq(range(Data), 5)), Person), nrow)
counts <- ddply(Group_df, .(cut(Data, breaks=fullseq(range(c(Data, max_range)), 5)), Person), nrow)
names(counts) <- c("Bin", "Person", "Frequency")
qplot(Person, Frequency, data = counts, fill = Person, geom="bar", stat="identity", width = 0.9, xlab="Person") +
facet_wrap(~ bin, drop = FALSE, nrow = 1) + scale_fill_manual(values = c("royalblue2", "tomato3", "palegreen2")) + opts(legend.position=c(0.8,0.70)) # + xlim("You", "Me", "Them")

# facet_grid(. ~ Bin) + scale_fill_manual(values = c("royalblue2", "tomato3", "palegreen2")) + opts(legend.position=c(0.8,0.70)) # + xlim("You", "Me", "Them")

--- On Tue, 3/31/09, Jason Rupert <jasonk...@yahoo.com> wrote:

hadley wickham

unread,
Mar 31, 2009, 10:09:55 AM3/31/09
to jasonk...@yahoo.com, ggp...@googlegroups.com, Mike Lawrence
Hi Jason,

The following code works ok for me:

qplot(Person, Frequency, data = counts, fill = Person, geom="bar",
stat="identity") +
facet_wrap(~ Bin, drop = FALSE)

which suggests that something else is going wrong. In general, it
would be really helpful if you could provide a minimal example that
reproduces the problem (as well as a similar version that is ok) - it
really helps me to quickly figure out where the bug lies.

Hadley
--
http://had.co.nz/

Jason Rupert

unread,
Mar 31, 2009, 11:31:19 AM3/31/09
to hadley wickham, ggp...@googlegroups.com, Mike Lawrence

On Windows (have not had a chance to try out on Mac OS:

It appears to be the capitable "B" in Bin vs. the lower case "b" in bin.

Works:
qplot(Person, Frequency, data = counts, fill = Person,
geom="bar", stat="identity") + facet_wrap(~ Bin, drop = FALSE)

Does not work:
qplot(Person, Frequency, data = counts, fill = Person,
geom="bar", stat="identity") + facet_wrap(~ bin, drop = FALSE)


Thanks again!
Reply all
Reply to author
Forward
0 new messages