簡體   English   中英

如何在 R 中使用 ggplot 的 face_wrap 分散 plot?

[英]How to scatter plot using face_wrap of ggplot in R?

我需要使用facet_wrap ggplot 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 Predicted 。 在此處輸入圖像描述

我們需要分別轉換每個 dataframe 然后 cbind 因為 x 是 Observed 而 y 是 Predicted,然后是 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 值添加到 plot 中,請參閱這篇文章中的答案

同樣,考慮使用基本 R 的reshape merge reshape 的數據幀(如果您是 package 作者,請避免任何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))

Plot

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~.)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM