Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filling under the curve with the stat function option #1528

skanskan opened this issue Jan 28, 2016 · 2 comments

filling under the curve with the stat function option #1528

skanskan opened this issue Jan 28, 2016 · 2 comments


Copy link

I would like to create a graph with the normal function from x=-2 to x=2 filled red under the curve from -2 to 0 and blue from 0 to 2.

I've tried
ggplot(NULL,aes(x=c(-2,2))) + geom_area(aes(x=c(-2,0)),stat="function", fun=dnorm, fill="red") +
geom_area(aes(x=c(0,2)),stat="function", fun=dnorm, fill="blue")

But it doesn't work as expected.
I know I could do it creating a x vector and a y vector of points and later using subset....
but how can I achieve it with the "stat" option?
I think it doesn't work, when you add a new area it doesn't respect whatever you did before.

I've also tried with the xmin and xmax option, without success.

It would also be nice to be able to specify two stat functions o values, one for ymax one for ymin, in order to easily paint the are between two curves.


@hadley hadley closed this as completed in ae1abff Jan 31, 2016
Copy link

hadley commented Jan 31, 2016

Here's a quick fix:

ggplot(NULL, aes(c(-2,2))) + 
  geom_area(stat = "function", fun = dnorm, fill = "red", xlim = c(-2, 0)) +
  geom_area(stat = "function", fun = dnorm, fill = "blue", xlim = c(0, 2))

Copy link

Great, thank you.
I've also figured out that I can mix it with the option "fill = NA" to leave some curves without shading.

Why do you need the first "c(-2,2)"?
Instead of specifying many geom_area sentences, it would be nice to use a single geom_area and define the fill and xlim subregions through a vector or list.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
None yet

No branches or pull requests

2 participants