Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于ggplot中的两个列表创建QQplot_R_Ggplot2_Statistics_Quantile - Fatal编程技术网

R 基于ggplot中的两个列表创建QQplot

R 基于ggplot中的两个列表创建QQplot,r,ggplot2,statistics,quantile,R,Ggplot2,Statistics,Quantile,我有两个列表,每个列表有四个数据帧。第一个列表(“loc_列表_OBS”)中的数据框只有两列“年”和“平均降水量”,而第二个列表(“loc_列表_模型”)中的数据框有33列“年”,然后是32个不同模型的平均降水量值 因此,loc_list_OBS中的数据帧如下所示,但数据将持续到2005年: Year Mean_Precip 1965 799.1309 1966 748.0239 1967 619.7572 1968 799.9263 1969 680.91

我有两个列表,每个列表有四个数据帧。第一个列表(“loc_列表_OBS”)中的数据框只有两列“年”和“平均降水量”,而第二个列表(“loc_列表_模型”)中的数据框有33列“年”,然后是32个不同模型的平均降水量值

因此,loc_list_OBS中的数据帧如下所示,但数据将持续到2005年:

Year     Mean_Precip
1965    799.1309
1966    748.0239
1967    619.7572
1968    799.9263
1969    680.9194
1970    766.2304
1971    599.5365
1972    717.8912
1973    739.4901
1974    707.1130
...     ....
2005    ....
loc_list_模型中的数据框如下所示,但共有32个模型列,数据也会追溯到2005年:

Year   Model 1      Model 2      Model 3    ...... Model 32
1965    714.1101    686.5888    1048.4274
1966    1018.0095    766.9161     514.2700
1967    756.7066    902.2542     906.2877
1968    906.9675    919.5234     647.6630
1969    767.4008    861.1275     700.2612
1970    876.1538    738.8370     664.3342
1971    781.5092    801.2387     743.8965
1972    876.3522    819.4323     675.3022
1973    626.9468    927.0774     696.1884
1974    752.4084    824.7682     835.1566
....    .....       .....         .....
2005    .....       .....         .....
每个数据帧表示一个地理位置,两个列表具有相同的四个位置,但一个列表用于观察值,另一个列表用于同一时间帧内的建模值

我想创建QQplot,将观测值的分位数与每个位置每个模型的分位数进行比较。我也想从一个pdf的每个位置的QQplot。我已经编写了将建模数据与标准正态分布进行比较的代码,并按照上面的规定创建了四个PDF。该守则如下:

for (q in loc_list) local({
  qq_combine_plot <- gather(q, condition, measurement, 2:33, 
                            factor_key = TRUE)
  ggplot(qq_combine_plot, aes(sample = measurement)) +
    facet_wrap(~ condition, scales = "free") +
    stat_qq() +
    stat_qq_line()+
    ggtitle(paste("qqplot for Mean Yearly Precip \n NE 2020-59 RCP45", 
                  names(q)))+
    theme(plot.title = element_text(hjust = 0.5))+
    labs(y = "Mean Yearly Precip (mm)")
  ggsave(file=paste("qq_NE_59_s45_", names(q), ".pdf"), 
         device = pdf, height = 14, width = 14)
})
用于(loc_列表中的q)本地({

qq\u combine\u plot如果我理解正确,您希望将第一个列表中的数据与第二个列表中的数据进行比较。然后为所有模型构建一个类似于
qqplot()
ggplot2
绘图。然后对每个城市的绘图进行区分并保存绘图(如果您有4个位置,您应该希望pdf中有4张幻灯片。)在下面的上下文中,我建议使用循环的下一种方法。您包含的步骤非常有用。为了比较两个数据帧,您必须在
gather()
操作之后连接它们。
qqplot()
可以计算值,我将其包含在代码中。此解决方案使用
tidyverse
函数完成,因此请检查您是否安装了id。最终输出将是pdf,但我已创建了一个列表(
list
),打印前在该列表中存储绘图。这里使用虚拟列表显示代码(使用本帖末尾的
df1
df2
创建)基于您共享的内容:

library(tidyverse)
#Code for data
#Data 1
List1 <- list(u1=df1,u2=df1,u3=df1,u4=df1)
#Data 2
List2 <- list(u1=df2,u2=df2,u3=df2,u4=df2)
最终输出将是您定义的某个目录中的pdf。只需注意
facet\u wrap()
。您可以使用所述函数具有的参数
nrow
ncol
来调整绘图中的列数和行数。下面是生成的pdf的一些输出:

使用的一些数据:

#Data 1
df1 <- structure(list(Year = c(1965L, 1966L, 1967L, 1968L, 1969L, 1970L, 
1971L, 1972L, 1973L, 1974L, 2005L), Mean_Precip = c(799.1309, 
748.0239, 619.7572, 799.9263, 680.9194, 766.2304, 599.5365, 717.8912, 
739.4901, 707.113, 707.113)), class = "data.frame", row.names = c(NA, 
-11L))

#Data 2
df2 <- structure(list(Year = c(1965L, 1966L, 1967L, 1968L, 1969L, 1970L, 
1971L, 1972L, 1973L, 1974L, 2005L), Model.1 = c(714.1101, 1018.0095, 
756.7066, 906.9675, 767.4008, 876.1538, 781.5092, 876.3522, 626.9468, 
752.4084, 752.4084), Model.2 = c(686.5888, 766.9161, 902.2542, 
919.5234, 861.1275, 738.837, 801.2387, 819.4323, 927.0774, 824.7682, 
824.7682), Model.3 = c(1048.4274, 514.27, 906.2877, 647.663, 
700.2612, 664.3342, 743.8965, 675.3022, 696.1884, 835.1566, 835.1566
)), class = "data.frame", row.names = c(NA, -11L))
#数据1

df1我还没能测试这个,但它看起来应该能工作!我会在测试时给你的答案打上正确的标记。@GarnerKohrell嗨,亲爱的!一定要做所有的检查并让我知道结果!!!
#Create an empty list to save the plots
List <- list()
#Loop any of List1 and List2 has the same length
for(i in 1:length(List1))
{
  x <- List1[[i]]
  y <- List2[[i]]
  #Text chain for names
  textchain <- names(List1[i])
  #First reshape data
  qq_combine_plot <- gather(y, condition, measurement, 2:dim(y)[2], 
                            factor_key = TRUE)
  #Now merge with original measure aka mean
  qqmer <- qq_combine_plot %>% left_join(x)
  #Now compute the qqplot measures
  r1 <- qqmer %>%
    group_by(condition) %>% 
    nest() %>% 
    mutate(qq = map(.x = data, ~as.data.frame(qqplot(x = .$Mean_Precip,
                                                     y = .$measurement, plot.it = FALSE)))) %>% 
    unnest(qq) 
  #Prepare plot
  G <- r1 %>%
    ggplot(aes(x = x, y = y)) + 
    geom_point() +
    facet_wrap(~condition,scales = 'free')+
    theme_bw()+theme(panel.grid = element_blank())+
    ylab("Model Quantile Values")+xlab("Observed Quantile Values")+
    ggtitle(paste0("qqplot for Mean Yearly Precip and modelled values between ",textchain," data"))
  #Assign to list
  List[[i]] <- G
}
#Export to pdf
pdf('Example.pdf',width = 14)
for(i in c(1:length(List)))
{
  plot(List[[i]])
}
dev.off()
#Data 1
df1 <- structure(list(Year = c(1965L, 1966L, 1967L, 1968L, 1969L, 1970L, 
1971L, 1972L, 1973L, 1974L, 2005L), Mean_Precip = c(799.1309, 
748.0239, 619.7572, 799.9263, 680.9194, 766.2304, 599.5365, 717.8912, 
739.4901, 707.113, 707.113)), class = "data.frame", row.names = c(NA, 
-11L))

#Data 2
df2 <- structure(list(Year = c(1965L, 1966L, 1967L, 1968L, 1969L, 1970L, 
1971L, 1972L, 1973L, 1974L, 2005L), Model.1 = c(714.1101, 1018.0095, 
756.7066, 906.9675, 767.4008, 876.1538, 781.5092, 876.3522, 626.9468, 
752.4084, 752.4084), Model.2 = c(686.5888, 766.9161, 902.2542, 
919.5234, 861.1275, 738.837, 801.2387, 819.4323, 927.0774, 824.7682, 
824.7682), Model.3 = c(1048.4274, 514.27, 906.2877, 647.663, 
700.2612, 664.3342, 743.8965, 675.3022, 696.1884, 835.1566, 835.1566
)), class = "data.frame", row.names = c(NA, -11L))