Home > OS >  Fill multiple areas under normal probability curve
Fill multiple areas under normal probability curve

Time:12-27

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.

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.

CodePudding user response:

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:

  • Related