Home > front end >  How to draw double coordinate CDF and PDF with R
How to draw double coordinate CDF and PDF with R

Time:05-06

I want to plot a picture with cumulative distribution function (CDF) and probability density function (PDF),which has a unified x-axis and y-axis with respective scale range on the left and right sides. I tried using the sec_axis() in ggplot2, but PR can only draw one line, and the Y-axis of the other line is invalid. What should I do?

new_data <- rnorm(n=100,mean=8000,sd=1000)
new_data <- as.data.frame(new_data)
names(new_data) <- c("CADP")

m1 <- ggplot(new_data,aes(x=CADP)) geom_density()
p1 <- ggplot(new_data, aes(x =CADP)) stat_ecdf(colour="red") 
           labs(
           x="CADP")

PR <- p1 scale_y_continuous(expand=c(0,0),limits=c(0,1),
                 sec.axis = sec_axis(~.*4e-04,breaks=seq(0,4e-04,1e-04))) 
                 geom_density(colour="blue")

CodePudding user response:

The two lines produced by stat_ecdf and geom_density are on vastly different scales. The cdf's peak value is about 2500 times the peak value of the pdf. If you want to see both lines clearly on the same plot, you need to apply a transformation to one of them, either dividing the cdf by about 2500 or multiplying the pdf by about 2500. You need to do this even if you have a secondary axis.

Remember that a secondary axis is just an inert annotation that gets stuck onto the side of the plot: it doesn't change the size, scale or shape of any of the objects you have plotted. The secondary axis is labelled in such a way that it allows you to 'pretend' that some of your data is on a different scale. The way this works is that you apply the transformation to your data to make it fit on your plot, and pass the inverse transformation as a function to sec_axis.

Although you can apply a transformation to the output of stat_ecdf, it is just as easy to create the transformed cdf yourself and plot it with geom_step

library(ggplot2)

new_data <- data.frame(CADP = rnorm(100, 8000, 1000))

ggplot(new_data,aes(x = CADP))   
  geom_density(colour = 'blue')  
  geom_step(aes(x = sort(CADP), 
                y = 0.0004 * seq_along(CADP)/nrow(new_data)))  
  scale_y_continuous(name = 'PDF',
                     sec.axis = sec_axis(~.x / 0.0004, name = 'CDF'))

enter image description here

  •  Tags:  
  • r
  • Related