Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

How to ggplot using facet_wrap functionality with multiple attribute

$
0
0

I am trying to create a three row facet_wrapggplot where i want to see both Calibration and Validation data but with their own legends. The code below gives me plot where i see Upstream, Midstream, and downstream for the calibration data of the DFplot_1 only. I want to have Validation data points for the same locations (upstream, midstream, and downstream) of DFplot_2data frame on the same figure but with different colors legends.In the past i have seen multiple variables on the same facet but can't figure out how to plot it here. I thought the higher the score would result in bigger circle. that means M2 has higher score so it should have been represented with a bigger circle. Any help would be appreciated.

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")

Here is the figure that i get from the code above enter image description here

Update: based on suggestions, i used facet_grid(Type ~ Variable) instead facet_wrap which sort of work but why i see wrong order? it seems faceting is done sequentially (starting with D (downstream) and ending with U (upstream))? Is there a way to reverse the order? here is the updated plot enter image description here


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>