将不同的QQ图(具有不同的数据集)放在同一坐标系中

时间:2016-10-21 13:05:27

标签: r fitdistrplus

我只能用不同的数据集逐个获得qq图。

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)

如何将两个qq图放在相同的坐标系中?

1 个答案:

答案 0 :(得分:1)

使用ggplot2。您需要从fitdist对象n中提取拟合值并创建新数据框。使用ggplot2图层添加两个数据集中的拟合值,然后添加一个参考值。

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)