如何在 R 中使用 face_wrap 的 ggplot 散点图?

How to scatter plot using face_wrap of ggplot in R?

我需要使用 ggplotfacet_wrap 功能 scatter plot 每个 Variable 的观察数据与预测数据。我可能很接近但还没有。我使用了我之前对 gather 数据问题的回答中的一些建议来自动化 plotting 过程。到目前为止,这是我的代码 - 我知道我的 ggplotaes 是错误的,但我故意使用它来阐明我的观点。我还想添加 geom_smooth 以获得 confidence interval.

library(tidyverse)
DF1 = data.frame(A = runif(12, 1,10), B = runif(12,5,10), C = runif(12, 3,9), D = runif(12, 1,12))
DF2 = data.frame(A = runif(12, 4,13), B = runif(12,6,14), C = runif(12, 3,12), D = runif(12, 4,8))

DF1$df <- "Observed"
DF2$df <- "Predicted"

DF = rbind(DF1,DF2)
DF_long = gather(DF, key = "Variable", value = "Value", -df)

ggplot(DF_long, aes(x = Observed, y = Predicted))+
  geom_point() +   facet_wrap(Variable~.)+ geom_smooth()

我应该看到如下所示的 plot,比较每个 VariableObserved Vs Predicted

我们需要分别转换每个数据帧,然后 cbind,因为 x 是观察到的,y 是预测的,然后是 facet,请看这个例子:

library(ggplot2)

# reproducible data with seed
set.seed(1)
DF1 = data.frame(A = runif(12, 1,10), B = runif(12,5,10), C = runif(12, 3,9), D = runif(12, 1,12))
DF2 = data.frame(A = runif(12, 4,13), B = runif(12,6,14), C = runif(12, 3,12), D = runif(12, 4,8))

DF1_long <- gather(DF1, key = "group", "Observed")
DF2_long <- gather(DF2, key = "group", "Predicted")
plotDat <- cbind(DF1_long, DF2_long[, -1, drop = FALSE])

head(plotDat)
#   group Observed Predicted
# 1     A 3.389578 10.590824
# 2     A 4.349115 10.234584
# 3     A 6.155680  8.298577
# 4     A 9.173870 11.750885
# 5     A 2.815137  7.942874
# 6     A 9.085507  6.203175


ggplot(plotDat, aes(x = Observed, y = Predicted))+
  geom_point() +
  facet_wrap(group~.) +
  geom_smooth()

我们可以使用 ggpubr 将 P 和 R 值添加到绘图中,请参阅 answers in this post:

同样,考虑 merge 使用基数 R 的 reshape 重塑数据帧(如果您是包作者,请避免任何 tidyr 依赖项)。下面 lapply + Reduce 动态合并以绕过辅助对象, DF1_longDF2_long , 在全局环境中:

数据

set.seed(10312019)

DF1 = data.frame(A = runif(12, 1,10), B = runif(12,5,10), 
                 C = runif(12, 3,9), D = runif(12, 1,12))
DF2 = data.frame(A = runif(12, 4,13), B = runif(12,6,14), 
                 C = runif(12, 3,12), D = runif(12, 4,8))

情节

library(ggplot2)      # ONLY IMPORTED PACKAGE

DF1$df <- "Observed"
DF2$df <- "Predicted"
DF = rbind(DF1, DF2)

DF_long <- Reduce(function(x,y) merge(x, y, by=c("Variable", "id")),
                  lapply(list(DF1, DF2), function(df) 
                       reshape(df, varying=names(DF)[1:(length(names(DF))-1)], 
                               times=names(DF)[1:(length(names(DF))-1)],
                               v.names=df$df[1], timevar="Variable", drop="df",
                               new.row.names=1:1E5, direction="long")
                  )
           )
head(DF_long)
#   Variable id Observed Predicted
# 1        A  1 6.437720 11.338586
# 2        A 10 4.690934  9.861456
# 3        A 11 6.116200  9.020343
# 4        A 12 6.499371  5.904779
# 5        A  2 6.779087  5.901970
# 6        A  3 6.499652  8.557102 


ggplot(DF_long, aes(x = Observed, y = Predicted)) +
  geom_point() + geom_smooth() + facet_wrap(Variable~.)