Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fill multiple areas under normal probability curve

I have a ggplot where I drew a normal probability curve and wanted to highlight multiple areas as below

library(dplyr)
library(ggplot2)
library(gghighlight)

df <- data.frame(x = seq(-5, 5, length.out = 100)) %>% mutate(y = dnorm(x))

ggplot(df, aes(x, y)) + geom_area(fill = "sky blue") + gghighlight(abs(x) < 1) + geom_area(fill = "red") + gghighlight(abs(x) < -1.6)

This plot does not work. However when I try to fill single area i.e.

`ggplot(df, aes(x, y)) + 
geom_area(fill = "sky blue") + gghighlight(abs(x)` < 1)` 

This works perfectly.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any consistent way to highlight multiple areas with different colours?

I also want to put regular legend in my plot to articulate definition of different colours i.e. sky blue = Area 01 and red = Area 02.

Any pointer will be very helpful.

>Solution :

You can try stat_function in ggplot2, I didn’t made it with gghighlight, sorry. It allows you to choose a function and a geom, so for your case with help of geom_area it seems to do the trick:

ggplot() + 
  xlim(-5, 5) + 
  stat_function(fun = dnorm, geom = "area", fill = "grey") + 
  stat_function(mapping = aes(fill = "Area 01"), fun = dnorm, xlim = c(-1, 1), geom = "area") + 
  stat_function(mapping = aes(fill = "Area 02"), geom = "area", fun = function(x) ifelse(abs(x) > 1.6, dnorm(x), NA)) + 
  scale_fill_manual(name = "Custom legend", values = c("skyblue", "red"))

For the first function, it’s just the standard normal distribution, the second is your highlighted with absolute value less than 1 so just use xlim, and the third can be done with 2 stat_function(…, xlim = ) but you can also pass a vectorized function.
You can specify a fill in mapping so that a legend is created, and then customize colors in scale_fill_manual.

Here is the result, I hope this is what you wanted:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading