r - coloring a segments of a curve when using stat_function in ggplot -



r - coloring a segments of a curve when using stat_function in ggplot -

i have plotted z-curve, , have used geom_segmentto color areas underneath curve.

i have plotted z distribution using

p <- ggplot(data.frame(x = c(-3, 3)), aes(x)) + stat_function(fun = dnorm)

i color segments of curve color of curve matches color on x-axis beneath it.

since i'm using stat_function, sense there less of chance modify it's characteristics.

has attempted similar feat , found way this?

dnorm_segment <- function(x, min = 0, max = 1) dnorm(x)*ifelse(x>=min & x<=max, 1, na) zero_segment <- function(x, min = 0, max = 1) ifelse(x>=min & x<=max, 0, na) plot_both <- function(min, max, colour) { args <- list(min = min, max = max) list( stat_function(fun = dnorm_segment, col = colour, size = 3, args = args, n = 1001), stat_function(fun = zero_segment, col = colour, size = 3, args = args, n = 1001) ) } ggplot(data.frame(x = c(-3, 3)), aes(x)) + stat_function(fun = dnorm) + plot_both(-3, -2, "purple") + plot_both(-2, -1, "yellow") # + etc

r colors ggplot2

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -