Search code examples
rfitdistrplus

Place different QQ plot (with different datasets) in the same coordinate system


I can only get the qq plot one by one with different datasets..

library(fitdistrplus)


x1<-c(1300,541,441,35,278,167,276,159,126,60.8,160,5000,264.6,379,170,251.3,155.84,187.01,850)
x2<-c(25,500,42,100,10,8.2,76,2.2,7.86,50)
y1<-log10(x1)
y2<-log10(x2)
x1.logis <- fitdist(y1, "logis", method="mle")
x2.logis <- fitdist(y2, "logis", method="mle")
ppcomp(x1.logis, addlegend=FALSE)
ppcomp(x2.logis, addlegend=FALSE)

How can i place the two qq plot in same coordinate system?


Solution

  • Use ggplot2. You need to extract your fitted values from the fitdist object n and make a new data frame. Use ggplot2 layers to add the fitted values from the two data sets and then add an abline.

    library(ggplot2)
    fittedx1 <- data.frame(x = sort(plogis(x1.logis$data,
                                    location = x1.logis$estimate[1], 
                                    scale = x1.logis$estimate[2])),
                           p = (1:length(x1.logis$data))/length(x1.logis$data))
    
    fittedx2 <- data.frame(x = sort(plogis(x2.logis$data,
                                    location = x2.logis$estimate[1], 
                                    scale = x2.logis$estimate[2])),
                       p = (1:length(x2.logis$data))/length(x2.logis$data))
    
    fitted <- rbind(fittedx1,fittedx2) #You need to combine the two datasets
    #Add a variable that identifies which dataset the values belong to
    #Then you can use the col option in ggplot to give each data set its own color!
    fitted$set <- c(rep("1", nrow(fittedx1)), rep("2", nrow(fittedx2))) 
    
    #Now plot
    ggplot(fitted) +
      geom_point(aes(p, x, col=set), shape=1, size=3) +
      geom_abline(intercept=0, slope=1)