stat_qq观察与建模ggplot2

时间:2019-03-25 18:01:26

标签: r ggplot2

我有两个变量,ObservedModelled。我想比较ModelledObserved的同意程度。我可以使用基数R来做到这一点,如Quantile-Quantile plot: compare multiple modelled variables vs one oberved variable ggplot2 R 所示。

提供了以下答案,但仍然针对Modelled分位数绘制Theoritical

    dat1 <- structure(list(Observed = c(0.091614725, 0.299650937, 1.212322967, 
2.08613786, 0.624578164, 0.875596703, 0.533393274, 2.570497051, 
0.90270844, 0.769422359, 0.108796032, 1.267789087, 2.480880866, 
0.293341894, 0.462380339, 2.181060779, 1.585163559, 1.721516188, 
1.449913667, 0.34820114), modelled1 = c(0.415608869, 1.660535391, 
0.63868133, 1.445153041, 0.094802484, 1.215438939, 0.645759518, 
0.891286508, 1.642724484, 0.260549858, 0.642058831, 1.285734544, 
2.018020137, 0.754278283, 2.292010155, 0.329381479, 0.57146522, 
0.371023885, 0.30127559, 0.405240147), modelled2 = c(0.086134967, 
0.453939895, 0.365002534, 0.577067364, 0.223386125, 0.590392351, 
0.906278888, 0.44978501, 1.793621353, 0.403286348, 1.390269802, 
0.317618089, 0.168787949, 0.12932007, 0.155591553, 0.663402964, 
0.410279472, 0.269137801, 0.641550456, 1.088450647), modelled3 = c(0.004554518, 
0.824550874, 1.170930069, 1.500506074, 0.191166637, 0.137615037, 
1.942911892, 0.102463799, 0.424826824, 0.957896996, 2.470568428, 
2.155577249, 0.550182645, 0.500102078, 1.301576456, 0.153277059, 
1.934976571, 0.323388916, 1.619773183, 0.985659845), modelled4 = c(0.071833305, 
0.222220781, 1.000684001, 0.598055075, 1.655253667, 1.364412981, 
0.292355944, 0.167764715, 1.028242365, 0.297494206, 0.003276404, 
0.504673001, 2.078326636, 0.317308159, 0.280580762, 0.399067913, 
0.683171302, 0.103172912, 1.169046094, 1.454754511), modelled5 = c(1.509321006, 
0.045986722, 0.132850681, 0.083444557, 0.37927173, 0.416340911, 
0.464467594, 1.830416293, 1.468343672, 2.413492849, 1.087170288, 
0.931888781, 0.098500168, 0.721654213, 1.377883717, 0.071488944, 
1.774245685, 1.817000972, 0.339679741, 0.542077424), modelled6 = c(0.142208046, 
0.133471032, 0.951323868, 0.3377028, 0.423486349, 0.402843531, 
0.000636775, 0.971852979, 1.91081828, 0.800451443, 1.801073348, 
2.528902074, 1.083330914, 0.048666588, 0.401656264, 0.00974592, 
0.261117902, 1.114048141, 1.001148946, 0.348547332), Town = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L), .Label = c("A", "B", "C", "D"), class = "factor")), .Names = c("Observed", 
"modelled1", "modelled2", "modelled3", "modelled4", "modelled5", 
"modelled6", "Town"), class = "data.frame", row.names = c(NA, 
-20L))

library(reshape2)

    data = melt(dat1, id.vars = "Observed")
         ggplot(data) +geom_qq(aes(sample=value, color = variable)) + 
          geom_qq_line(aes(sample=Observed))+
          facet_wrap(~variable)

如何使用ggplot2生成以下所示的Q-Q图(仅作为示例)?

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以将qqplotplot.it = FALSE一起使用来进行qqplot的计算,然后使用ggplot对其进行绘制。

library(tidyverse)
dat1 %>% 
  gather(key = model, value = value, -Observed) %>% 
  group_by(model) %>% 
  nest() %>% 
  mutate(qq = map(.x = data, ~as.data.frame(qqplot(x = .$Observed, y = .$value, plot.it = FALSE)))) %>% 
  unnest(qq) %>% 
  ggplot(aes(x = x, y = y)) + 
  geom_point() +
  facet_wrap(~model)

您可以使用geom_abline()

添加行