Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different geom_rect() objects for facets

Tags:

r

ggplot2

facet

I have a dataframe which I've used to create a ggplot object faceted into 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 put geom_rect() objects on each of these facets, but with different ymin and ymax values for each plot.

Here's 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 plot:

Part completed graph

I've got my thresholds (x and y limits for geom_rect() objects) in a separate dataframe as follows:

    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

Dput 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 have added this code snippet 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 as follows:

Error: ggplot2 doesn't know how to deal with data of class factor

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 either be length one, or the same length as the dataProblems:xmin, xmax, ymin, ymax

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

like image 553
s_boardman Avatar asked Aug 13 '14 12:08

s_boardman


1 Answers

So you didn't provide us with labels, and only three rows of the first data set, so what follows is incomplete, but should demonstrate how to get the rect's working:

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 were using $ in the first aes() call. Never do that. Then you need to un-map x and y in the geom_rect layer, since they are inherited from the top level ggplot call. The other option would be to use inherit.aes = FALSE.

like image 144
joran Avatar answered Oct 07 '22 01:10

joran