1
votes

I've run into a problem, that I can't seem to solve, and after having searched the internet for 24h I decided to ask in here.

I have a 4 boxplot figure, that I made with the ggplot2. Each boxplot is named after the data variable. But to make it easier for the reader to understand, I would like to change the title of each single boxplot (without changing the name in the data set). Is this possible? I've tried using "ggtitle", "rename", "main=" and several other suggestions, I found on the internet.

Here is the coding I've done:

d11 <- droplevels(subset(d, time=="Baseline" | time=="Pre-ECT" | time=="1st Period Post-ECT" | time=="2nd Period Post-ECT"))

ggplot(melt(d11, id.vars= c("subj", "time", "time.num", "intervention","order", "age", "sex", "diagn", "diagn2"), measure.vars = c("HR", "BPsys", "BPdia", "CO")), aes(x=time, y = value, fill = intervention)) + 
  geom_boxplot()  +
  theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0.5)) + 
  facet_wrap(~variable, scales= "free")

4 boxplots

I would like the headings in the boxplots to be "Heart rate", "Systolic blood pressure", "Diastolic blood pressure", "Cardiac output" instead.

Any suggestions to a function, that might work?

1
look at the labeller argument to facet_wrap - Richard Telford
Hi Richard. Thank you for your comment. It's seems as if I use the .default argument, I can make all the labels disappear, but I'm having trouble in replacing them with my own names. - Søren Thinus Just Christensen

1 Answers

2
votes

You need to use the labeller argument in facet_wrap. The easiest way to use it is with as_labeller and a named vector.

ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point() +
  facet_wrap(~Species, 
    labeller = as_labeller(c("setosa" = "I. setosa", "versicolor" = "I. versicolor",  "virginica" = "I. virginica")))