Using ggplot2's stat_ecdf( ) function, I have made a Cumulative Density Function plot. I am required to shade area under the CDF curve between two x-axis values and convert it to plotly output. Using the IRIS dataset, I have replicated the scenario with the following code:
library(ggplot2)
iris <- datasets::iris
iris <- iris[order(iris$Sepal.Length),]
(plot_1 <- ggplot(iris, aes(Sepal.Length)) +
stat_ecdf() +
scale_x_reverse())
plot_1_plotly <- ggplotly(plot_1)
plot_1_plotly
(plot_2 <- ggplot(iris, aes(Sepal.Length)) +
stat_ecdf(aes(ymin = 0, ymax = ..y..), geom = "ribbon", alpha = 0.2,
fill = "blue") +
stat_ecdf(geom="step") +
scale_x_reverse())
plot_2_ggplotly <- ggplotly(plot_2)
plot_2_ggplotly
- plot_1 produces this output, which is a normal CDF curve (not shaded)
- plot_1_plotly produces this output, which is the plotly version (not shaded)
- plot_2 produces this output, which is my attempt at getting the area under the curve shaded (with help from the answer to this question)
- plot_2_plotly produces this output, which is the plotly version of plot_2
Question 1: In plot_2 output, how do I restrict the shaded area between two x-axis values (say x = 6 and x = 7)?
Question 2: When I convert plot_2 to plotly output i.e plot_2_plotly, why does the shaded area get messed up as shown in the output? How to get back to original form?