Search code examples
rggplot2facet-wrap

Varying facet title in ggplot


I have the following data frame:

df = structure(list(Age = c(85.3008017936344, 85.3008017936344, 
85.3008017936344, 
85.3008017936344, 57.8700505396495, 57.8700505396495, 57.8700505396495, 
57.8700505396495, 57.8700505396495, 72.7514762882082, 72.7514762882082, 
72.7514762882082, 72.7514762882082), ID = c("37", "37", "37", 
"37", "38", "38", "38", "38", "38", "39", "39", "39", "39"), 
TestResult = c(13.57123199512, 31.915548177134, 37.956556605893, 
31.9504259460811, 6.11843008606919, 41.3992419385478, 31.0176354057889, 
37.3284699517338, 40.3868204693566, 13.2087095959212, 37.3690920066042, 
34.3923572397983, 45.2206934197729), Time = c(0, -0.25, -1.25, 
-2.75, 0, -0.25, -0.75, -1.25, -1.75, 0, -0.25, -0.75, -1.25
)), row.names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame"
))

I would like to plot Test result versus time for each patient, and I want the facet title to contain the age of the patient.

I tried the following:

ggplot(df, aes(Time, TestResult)) +
  geom_point() +
  geom_line() + 
  facet_wrap(~ID, labeller = as_labeller(df$Age))

but it seems to ignore the labeller.

Any ideas?


Solution

  • You can just use age for the facet.

    ggplot(df, aes(x=Time, y=TestResult)) +
       geom_line() +
       geom_point() +
       facet_wrap(~Age)
    

    image