Hadley
> --
> 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
>
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
I don't know what 'works' means to you, but there are several ways one
can interpret your code. Here are a few attempts that 'work', but
yield different outcomes:
# Use geom_abline():
d <- data.frame(x = rnorm(50))
testPlot <- function(df) {
print(ggplot(df, aes(x = x)) +
geom_histogram(binwidth = 0.05, fill = 'lightblue') +
geom_abline(intercept = 1, slope = -1, size = 1) )
}
testPlot(d)
This has the effect of fitting a line of slope -1 throughout the
plotting region. If you want the line's extent to be restricted to the
unit square, then you can create a data frame of coordinates and pass
that in as the data in the layer that produces the line, as Hadley
suggested:
d <- data.frame(x = rnorm(50))
coords <- data.frame(x = seq(0, 1, 0.1), y = seq(1, 0, -0.1))
# coords <- data.frame(x = c(0, 1), y = (1, 0)) ## gets the same line
ggplot() +
geom_histogram(data = d, aes(x = x), binwidth = 0.05, fill = 'lightblue') +
geom_line(data = coords, aes(x = x, y = y), size = 1)
If you prefer a density histogram over a frequency histogram, then use
ggplot() +
geom_histogram(data = d, aes(x = x, y = ..density..),
binwidth = 0.05, fill = 'lightblue') +
geom_line(data = coords, aes(x = x, y = y), size = 1)
Sometimes it's necessary to resort to <<- , but it shouldn't be used
unless you know what you're doing (and even then, you should usually
rethink why you're using it). You resort to this ploy because x and y
are not 'visible' to ggplot() inside the print() command of your
function unless they exist in the global environment, which
demonstrates a fundamental misunderstanding on your part of how data
are passed in ggplot(). Firstly, it expects data frames. Always. This
is well documented. Secondly, you can import multiple data frames with
different data in different layers. This is one of the features that
gives ggplot() more power and flexibility. The code above illustrates
the idea: the ggplot() call has an empty default data frame, which
requires each layer to have its own data = argument. In this case, the
data frame d is used to generate the histogram while the data frame
coords is used to generate the line.
One can also have a default data frame and pass a new data frame into
a specific layer; the following addition of geom_rug() is a little
silly in this context, but the point is that a geom will look for its
input data from the default data frame unless told otherwise by
specification of data = within a geom statement:
ggplot(d) +
geom_histogram(aes(x = x, y = ..density..),
binwidth = 0.05, fill = 'lightblue') +
geom_rug(aes(x = x)) +
geom_line(data = coords, aes(x = x, y = y), size = 1)
ggplot() defines the defaults for each layer in the plot: more
specifically, the default data frame and default aesthetics that are
expected to be present in all successive layers. In this example, x is
a common aesthetic (although it doesn't have the same values in the
line geom) but y is not, so we could shorten the code a bit by making
x a default aesthetic, but not y:
ggplot(d, aes(x = x)) +
geom_histogram(aes(y = ..density..),
binwidth = 0.05, fill = 'lightblue') +
geom_rug() +
geom_line(data = coords, aes(x = x, y = y), size = 1)
Since x is to be redefined in the geom_line() layer, it needs to be
stated as an aesthetic in reference to the coords data frame. Try it
without x and see what happens.
To wrap all this inside a function,
testPlot2 <- function(df1, df2) {
print(
ggplot(data = df1, aes(x = x)) +
geom_histogram(aes(y = ..density..),
binwidth = 0.05, fill = 'lightblue') +
geom_rug() +
geom_line(data = df2, aes(x = x, y = y), size = 1)
)
}
testPlot2(d, coords)
It also occurred to me that you could use geom_segment() to produce
the line just as easily, thus dispensing with the coords data frame
altogether:
ggplot(d) +
geom_histogram(aes(x = x, y = ..density..),
binwidth = 0.05, fill = 'lightblue') +
geom_segment(aes(x = 0), xend = 1, y = 1, yend = 0)
Even though x is not variable within geom_segment(), it needs to be
wrapped in aes() because it is not the same x as that in the default
data frame d, which geom_segment() is reading by default. In fact, I
found four ways to get geom_segment() to produce the same line: two
using aes() and two with external one line data frames. The second
aes() statement I used was
geom_segment(aes(x = 0, xend = 1, y = 1, yend = 0), legend = FALSE)
Take out the legend = FALSE line, see what happens and ask yourself
why that occurred.
HTH,
Dennis
>> To unsubscribe: email ggplot2+u...@googlegroups.com
>> More options: http://groups.google.com/group/ggplot2
>>
>
>
>
> --
> Assistant Professor / Dobelman Family Junior Chair
> Department of Statistics / Rice University
> http://had.co.nz/
>
> --
> 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