filling up to the edge + 'where' is missing

29 views
Skip to first unread message

Ross Boylan

unread,
May 2, 2014, 9:40:56 PM5/2/14
to ggplot2
I want to make a graph of a linear probability model, coloring the
background red for areas where the probability is an impossible value.

nofly <- data.frame(x=c(-1, -1, 3, 3, -1, -1, 3, 3),
y=c(-1, 0, 0, -1, 1, 2, 2, 1),
id=rep(1:2, each=4))
g <- ggplot(data=data.frame(x=0, y=.2), aes(x=x, y=y)) +
scale_y_continuous("Probability", limits=c(-1, 2)) +
scale_x_continuous(limits= c(-1, 3)) +
geom_point() +
geom_polygon(data=nofly, fill="red", alpha=0.3, aes(group=id)) +
geom_abline(intercept=0.2, slope=0.8)

This basically works, but there are 2 annoyances.

First, the area that is red does not extend out to the x limits of the
background, which go beyond the explicit limits I set. Is there a way
to get it to extend all the way?

Second, I didn't really want to plot the point. But everything I tried
without it produced the error
> g <- ggplot(data=data.frame(x=0, y=.2), aes(x=x, y=y)) +
+ geom_abline(intercept=0.2, slope=0.8)
> g
Error in as.environment(where) : 'where' is missing

Is there a way to get around that? A search found quite a few messages
discussing such error messages, but they seemed to have different causes
than mine (though perhaps just on the surface).

Thanks.
Ross Boylan

Dennis Murphy

unread,
May 3, 2014, 12:05:23 AM5/3/14
to Ross Boylan, ggplot2
The argument expand = in scale_*_continuous() controls the padding;
setting expand = c(0, 0) removes it. If you don't want points, don't
use geom_point(). Here's the simplest way I know of to reproduce your
plot sans the point:

nofly <- data.frame(x=c(-1, -1, 3, 3, -1, -1, 3, 3),
y=c(-1, 0, 0, -1, 1, 2, 2, 1),
id=rep(1:2, each=4))

library(ggplot2)
ggplot() +
geom_polygon(data=nofly, fill="red", alpha=0.3,
aes(x = x, y = y, group=id)) +
scale_y_continuous("Probability", expand = c(0, 0)) +
scale_x_continuous(expand = c(0, 0)) +
geom_abline(intercept=0.2, slope=0.8, size = 1)

The aesthetics you want are in nofly, so you can define them in the
geom_polygon() layer. It is legitimate to pass an empty argument list
to ggplot(), which will set up an empty base layer. Since
geom_abline() doesn't explicitly need a data frame when the intercept
and slope are vectors of length 1 (i.e., they are set rather than
mapped), you have everything you need. All I did was add a size
argument to geom_abline() to make it a bit thicker.

The following also works because geom_abline() doesn't use any of the
aesthetics defined in the ggplot() call and only one data frame is in
play:

ggplot(data=nofly, aes(x = x, y = y, group=id)) +
geom_polygon( fill="red", alpha=0.3) +
scale_y_continuous("Probability", expand = c(0, 0)) +
scale_x_continuous(expand = c(0, 0)) +
geom_abline(intercept=0.2, slope=0.8, size = 1)


Dennis
> --
> --
> 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/d/optout.
Reply all
Reply to author
Forward
0 new messages