Various geom_rect () objects for faces

I have a dataframe that I used to create a ggplot object allocated to three separate plots.

max_24h_lactate_cpet.long

First_24h_Lactate_Max,  Lactate_Above_Threshold,           Metric,           Value
2.3,                    High,                              AT_VO2_mL.kg.min, 17.00
2.3,                    High,                              VO2_Peak,         84.07
2.3,                    High,                              AT_VE_VCO2,       35.00

      

In dput format:

dput(max_24h_lactate_cpet.long)
structure(list(First_24h_Lactate_Max = c(2.3, 2.3, 2.3), Lactate_Above_Threshold = structure(c(1L, 
1L, 1L), 
.Label = c("High", "Normal"), class = "factor"), Metric = structure(1:3, .Label = c("AT_VO2_mL.kg.min", 
"VO2_Peak", "AT_VE_VCO2"), class = "factor"), Value = c(17, 84.07, 
35)), .Names = c("First_24h_Lactate_Max", "Lactate_Above_Threshold", 
"Metric", "Value"), row.names = c(44L, 192L, 340L), class = "data.frame")

      

I want to place geom_rect () objects on each of these faces, but with different ymin and ymax values ​​for each graph.

Here is my current code:

max_24h_lac_vs_cpet <- ggplot(max_24h_lactate_cpet.long, 
                              aes(x = max_24h_lactate_cpet.long$First_24h_Lactate_Max, 
                                  y = max_24h_lactate_cpet.long$Value))

max_24h_lac_vs_cpet + geom_point() +
  facet_wrap( ~ Metric, scales="free_y") +
  scale_color_brewer(palette="Set1") +
  labs(x = "Max Lactate Value < 24h after surgery (mmol)", 
       y = "Test Metric Value") +
  stat_smooth(method="lm") +
  annotate("rect", xmin=-Inf, xmax=1.6, ymin=-Inf, ymax=Inf,alpha=0.1,fill="blue")

      

This gives the following graph:

Part completed graph

I have my thresholds (x and y constraints for geom_rect () objects) in a separate dataframe like this:

    Metric              xmin    xmax    ymin    ymax
    AT_VO2_mL.kg.min    -Inf    Inf     -Inf    10.2
    VO2_Peak            -Inf    Inf     -Inf    75.0
    AT_VE_VCO2          -Inf    Inf     42      Inf

      

Error code:

dput(thresholds)
structure(list(Metric = structure(c(2L, 3L, 1L), .Label = c("AT_VE_VCO2", 
"AT_VO2_mL.kg.min", "VO2_Peak"), class = "factor"), xmin = c(-Inf, 
-Inf, -Inf), xmax = c(Inf, Inf, Inf), ymin = c(-Inf, -Inf, 42
), ymax = c(10.2, 75, Inf)), .Names = c("Metric", "xmin", "xmax", 
"ymin", "ymax"), class = "data.frame", row.names = c(NA, -3L))

      

And added this piece of code to my ggplot call

 + geom_rect(data=thresholds$Metric, aes(xmin=xmin, xmax=xmax, 
                                         ymin=ymin, ymax=ymax, 
                                         alpha=0.1,fill="red"))

      

Which gives an error like this:

Error: ggplot2 doesn't know how to handle class factor data

Using the following also gives an error:

 + geom_rect(data=thresholds, aes(xmin=xmin, xmax=xmax, 
                                  ymin=ymin, ymax=ymax, 
                                  alpha=0.1,fill="red"))

      

Error: Aesthetics must be either the same or the same length as dataProblems: xmin, xmax, ymin, ymax

I've looked at examples from other questions , but I'm trying to translate their answers to my own problem. Any help would be appreciated!

+3


source to share


1 answer


So you didn't provide us labels

with just three lines of the first dataset, so the following is incomplete, but should demonstrate how to get rect to work:

max_24h_lac_vs_cpet <- ggplot(max_24h_lactate_cpet.long, 
                              aes(x = First_24h_Lactate_Max, 
                                  y = Value))

max_24h_lac_vs_cpet + geom_point() +
  facet_wrap( ~ Metric, scales="free_y") +
  scale_color_brewer(palette="Set1") +
  labs(x = "Max Lactate Value < 24h after surgery (mmol)", 
       y = "Test Metric Value") +
  stat_smooth(method="lm") + 
  geom_rect(data=thresholds, aes(x = NULL,y = NULL,xmin=xmin, xmax=xmax, 
                                  ymin=ymin, ymax=ymax, 
                                  alpha=0.1,fill="red"))

      



You used $

in the first call aes()

. Never do this. Then you need to format x

and y

at the level geom_rect

, as they inherit from the top level of the ggplot

call. Another option is to use inherit.aes = FALSE

.

+3


source