Я столкнулся с подобной проблемой, пытаясь затенить область кривой CDF для экспоненциальной функции выживания.Используя geom_polygon
, я смог найти решение для линейного графика CDF.
# creating poisson distribution with mean of 15 and cumulative count/ proportion
cumulative_frequencies <- data.frame(person_id=1:100,
num_active_days=rpois(10000, lambda=15)) %>%
group_by(num_active_days) %>% summarise(num_people = n()) %>%
arrange(num_active_days) %>%
mutate(cum_frequency=cumsum(num_people),
rel_cumfreq = cum_frequency/sum(num_people))
# create cdf curve
p <- ggplot(cumulative_frequencies, aes(x=num_active_days, y=rel_cumfreq)) +
geom_line() +
xlab("Time") +
ylab("Cumulative Density") + theme_classic()
p
Затем затенение в нужной области подкривая с использованием geom_polygon
:
# minimum value of x for the area under the curve shading
x_start <- 15
x_end <- 20
#Subset the data and add the coordinates to make it shade to y = 0
shade <- rbind(c(x_start,0), subset(cumulative_frequencies, num_active_days >=
x_start & num_active_days <= x_end), c(x_end, 0))
# add shading to cdf curve
p + geom_polygon(data = shade, aes(num_active_days, rel_cumfreq))