Cdplot () analog in ggplot2

I am looking for a graph of conditional density, as a built-in function cdplot, but using ggplot2.

Here is an example of vanilla cdplot:

with(iris, cdplot(Sepal.Length, Species))

cdplot example

The ggplot2book (p. 188) states that the following calls should be equivalent:

cdplot(x, y)
qplot(x, fill=y, geom="density", position="fill")

However, it seems that this behavior was violated when upgrading to ggplot2(it also gives a warning `position` is deprecated):

with(iris, qplot(Sepal.Length, fill=Species, geom="density", position="fill"))

ggplot example

I found a blog entry about who is trying to do the same , but apparently now is also broken (this is a warning, `position` is deprecated):

cdens <- cdplot(iris$Sepal.Length, iris$Species, plot = F)
x <- seq(min(iris$Sepal.Length), max(iris$Sepal.Length), length.out = 100)
y <- c(cdens[[1]](x), cdens[[2]](x), rep(1, length(x)))
type <- ordered(rep(levels(iris$Species), each = length(x)),
                levels=rev(levels(iris$Species)))
x <- rep(x, 3)
qplot(x, y, geom="area", fill = type, position="identity",
      xlab="Sepal.Length", ylab="Species") + theme_bw()

enter image description here

How can this be done? What is broken in these examples?

(I want a solution ggplotbecause it has better labeling and axis legends, especially when the independent variable is a date.)

: , @bouncyball ggplot(iris, aes(x = Sepal.Length, fill = Species))+ geom_density(position = 'fill'), - :

with(data, cdplot(time, cat))
abline(v=as.POSIXct(c('2017-04-01', '2017-03-01')), col='red')

enter image description here

ggplot(data, aes(x=time, fill=cat)) + geom_density(position = 'fill')

enter image description here

cdplot - , , , ggplot. cdplot , , 2017 :

> with(subset(data, time>'2017-03-01' & time <'2017-04-01'), table(cat))
cat
   <1s    <3s    <5s   <10s   <20s    <1m    <2m    <5m    <1h   <24h    >1d 
175484  31837  19078  16146  15013  20200   1142   1207    944     17      0 
+4

Source: https://habr.com/ru/post/1687386/


All Articles