如何使用具有多个属性的facet_wrap功能进行ggplot

问题描述 投票:0回答:1

我正在尝试创建一个三行facet_wrap ggplot,我想在其中同时看到校准和验证数据,但要有自己的legends。下面的代码为我提供了plot,其中仅DFplot_1的校准数据位于上游,中游和下游。我想在同一DFplot_2上具有data frame figure的相同位置(上游,中游和下游)的验证数据点,但颜色legends不同。过去我看到过多个variables在相同的facet上,但无法在此处找出如何plot。我认为分数越高,圈数越大。这意味着M2得分较高,因此应该以更大的圆圈表示。任何帮助,将不胜感激。

library(tidyverse)

DF_1 = data.frame(
    Models = c("M1", "M2","M3","M4"), 
    Cost = c(4,44.75,9.28, 6.7), 
    Upstream = c(0.70,0.80,0.73,0.70), 
    Midstream = c(0.78,0.82,0.79,0.65), 
    Downstream = c(0.56,0.85,0.57,0.80), 
    Type = rep("Calibration", 4)
)  

DFPlot_1 = gather(data = DF_1, key = "Variable", value = "Value", -Cost, -Models, -Type)

DF_2 = data.frame(
    Models = c("M1", "M2","M3","M4"), 
    Cost = c(4,44.75,9.28, 6.7), 
    Upstream = c(0.70,0.80,0.73,0.70), 
    Midstream = c(0.78,0.82,0.79,0.65), 
    Downstream = c(0.56,0.85,0.57,0.80), 
    Type = rep("Validation", 4)
)  

DFPlot_2 = gather(data = DF_2, key = "Variable", value = "Value", -Cost, -Models, -Type)
DF = rbind(DFPlot_1, DFPlot_2)

ggplot(data = DF, aes(x = Cost, color = Models, size = Models)) +
  geom_point(aes(y = Value)) + 
  facet_wrap(~ Variable, nrow = 3) +
  theme_bw() + 
  theme( 
    strip.text.x = element_text(size = 14), 
    text = element_text(size=20, face = "bold"),
    legend.position = c(0.55, 0.85),
    legend.direction = "horizontal", 
    legend.title = element_blank()
  ) + 
  labs(x = "\nComputational cost (Hrs)", y = "KGE\n")

这是我从上面的代码中得到的数字enter image description here

r ggplot2 plot facet-wrap
1个回答
-1
投票

[您的模型向量未设置为'因子',因此R不知道它们所处的'顺序'或排名。搜索这些术语,您将一路走来。

© www.soinside.com 2019 - 2024. All rights reserved.