0

I want to draw CDF and PDF plots with different axes. Here is my data;

data<-c(`1979` = 12.03, `1980` = 13.66, `1981` = 14.19, `1982` = 11.7, 
`1983` = 11.68, `1984` = 11.78, `1985` = 11.19, `1986` = 12.49, 
`1987` = 16.22, `1988` = 11.51, `1989` = 10.52, `1990` = 11.53, 
`1991` = 10.5, `1992` = 11.2, `1993` = 12.53, `1994` = 10.17, 
`1995` = 11.34, `1996` = 12.22, `1997` = 12.84, `1998` = 13.17, 
`1999` = 12.61, `2000` = 11.33, `2001` = 11.47, `2002` = 11.75, 
`2003` = 12.52, `2004` = 13.03, `2005` = 11.8, `2006` = 12.78, 
`2007` = 10.83, `2008` = 14.72, `2009` = 13.18, `2010` = 11.71, 
`2011` = 10.21, `2012` = 14.33, `2013` = 12.83, `2014` = 10.84, 
`2015` = 11.97, `2016` = 10.65, `2017` = 11.71, `2018` = 11.61, 
`2019` = 13.3, `2020` = 11.71)

my codes:

par(mar = c(5, 4, 4, 4) + 0.3)   
plot(ecdf(data), ylab="CDF", xlab="MIWH", main = "y")      
par(new = TRUE)
pdf<-dnorm(data, mean(data),sd(data))
plot(data,pdf, col = 3, axes = FALSE, xlab = "", ylab = "")
axis(side = 4,  at = pretty(range(pdf)))
mtext("PDF", side = 4, line = 3)

I am getting:

enter image description here

But I desire green points to see like a curve:

enter image description here

I tried curve function, but with it I am not able to add axis of pdf.

2
  • 1
    Set type = "l" for lines instead of points inside of plot(). Or use The lines() function instead of the plot() function. Commented May 27, 2021 at 13:41
  • @GregorThomas I tried both but I am getting unreasonable plots. Commented May 27, 2021 at 13:47

1 Answer 1

1

I don't understand why you wouldn't rather plot the normal CDF (with the same y axis).

par(mar = c(5, 4, 4, 4) + 0.3)   
plot(ecdf(data), ylab="CDF", xlab="MIWH", main = "y", xlim = c(9, 17))   
par(new = TRUE)
curve(dnorm(x, mean = mean(data), sd = sd(data)),
      col = 3, xlim = c(9, 17), axes = FALSE, xlab = "", ylab = "")

axis(side = 4,  at = pretty(c(0, dnorm(0, sd = sd(data)))),
     col = 3, col.axis = 3)
mtext("PDF", side = 4, line = 3)

resulting plot

Sign up to request clarification or add additional context in comments.

5 Comments

I tried that but then I noticed that y axis of CDF is not matching values of PDF. Maybe I made a mistake while doing it.
I don't think you understand. I mean you should plot the CDF instead of the PDF. Then you would only need one y-axis and comparison between the empirical and theoretical distribution would be easier. OTOH, you could simply make a qq-plot.
Oh I got it now. Actually my idea was to do what you said then my advisor also wanted me to add PDF.
You could also plot a kernel density estimate (see density) instead of the empirical CDF.
I am trying it, appreciate for your advices and solutions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.