I was looking at writing a convenience function for creating
histograms of integer elements (and also provide an example in
response to Hadley's request), but my function seems to lock up R.
Here's the function:
library(ggplot2)
intHist <- function(x, xlab=xlab, main=main) {
d <- data.frame(x=x)
r <- range(x)
print(qplot(x, data=d, breaks=seq(r[1] - 0.5, r[2] + 0.5, by=1),
xlab=xlab, main=main, geom="histogram"))
}
After defining this, in R 2.8.1 with ggplot2 0.8.1 on a MacBook Pro,
executing the following causes R to freeze:
intHist(c(1,1,2,2,2))
Any ideas? Thanks.
David
Thank you. I think the only difference is that I want the breaks to be
shifted slightly so that the input values are not on the boundaries of
the intervals. For integer data in particular, I find the plot to be
clearer this way.
Running the same code under R 2.7.2, my function exits with an error
rather than freezing up:
> intHist(c(0,0,0,1,1))
Error in qplot(x, data = d, breaks = seq(r[1] - 0.5, r[2] + 0.5, by = 1), :
promise already under evaluation: recursive default argument
reference or earlier problems?
I found that switching to using the method you described is still
freezing up R 2.8.1 and also produces the above error message in R
2.7.2. (I think they are equivalent under the covers.)
intHist <- function(x, xlab=xlab, main=main) {
d <- data.frame(x=x)
print(qplot(x, data=d, xlab=xlab, main=main, geom="histogram") +
scale_x_continuous(breaks = seq(min(x), max(x), 1)))
}
> intHist(c(0,0,0,1,1))
Error in qplot(x, data = d, xlab = xlab, main = main, geom = "histogram") :
promise already under evaluation: recursive default argument
reference or earlier problems?
Sounds kind of funky.
David
Hi David :-)
Thank you. I think the only difference is that I want the breaks to be
shifted slightly so that the input values are not on the boundaries of
the intervals. For integer data in particular, I find the plot to be
clearer this way.
Running the same code under R 2.7.2, my function exits with an error
rather than freezing up:intHist(c(0,0,0,1,1))Error in qplot(x, data = d, breaks = seq(r[1] - 0.5, r[2] + 0.5, by = 1), :
promise already under evaluation: recursive default argument
reference or earlier problems?
I found that switching to using the method you described is still
freezing up R 2.8.1 and also produces the above error message in R
2.7.2. (I think they are equivalent under the covers.)
intHist <- function(x, xlab=xlab, main=main) {
d <- data.frame(x=x)
print(qplot(x, data=d, xlab=xlab, main=main, geom="histogram") +
scale_x_continuous(breaks = seq(min(x), max(x), 1)))
}intHist(c(0,0,0,1,1))Error in qplot(x, data = d, xlab = xlab, main = main, geom = "histogram") :
promise already under evaluation: recursive default argument
reference or earlier problems?
qplot(factor(x), geom = 'histogram', xlab = 'x axis', main = 'main
title')
That way you should be able to use pretty much whatever you want as
easily as possible. :)
david.
David