Search code examples
rggplot2annotategeom-text

Adding variable name outside of panel/plot


This is how the plot looks, the text I want to add should be outside the panel to the leftSample-data:

df <- data.frame("SL" = runif(50, 2.2, 5.8), "LMX" = runif(50, 1.8, 5.5))

I have many different variables for each of which I want to make a box plot with the code below. So that all panels will have the same size, I determined the plot margin so that it is not influenced by the length of the name of the variable. Therefore, now I want to add the variable name outside of the panel to the left. However, this turns out to be more difficult than expected. I know that this issue has been raised before, but none of the solutions works with me (rnorm or geom_text). Any help is much appreciated, thank you :)

df %>%
select("Servant Leadership" = SL) %>%
gather(key = "variable", value = "value") -> n
n$variable <- factor(n$variable, levels = c("Servant Leadership"))


ggplot(data = n, aes(y = value, x = as.numeric(variable))) +
stat_summary(fun.data = min.mean.sd.max, geom = "boxplot", col = "#323232", fill = "#EFC76C") + 
scale_fill_identity() + 
scale_x_continuous(breaks = as.numeric(unique(n$variable)), minor_breaks = NULL,
                 labels = "", expand = c(0.12, 0.12)) + 
scale_y_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7)) +
expand_limits(y = c(1, 7)) + coord_flip() + labs(x = "", y = "") +
theme(text = element_text(size = 15), panel.background = element_rect(fill = "#EAEDED"), 
    panel.border = element_rect(fill=NA, color = "grey", size = 0.5, linetype = "solid")) +
theme(plot.margin=unit(c(0.2, 0.2, 0, 4),"cm"))

I forgot this code which I ran before:

min.mean.sd.max <- function(x) {
r <- c(min(x), mean(x) - sd(x), mean(x), mean(x) + sd(x), max(x))
names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
r
}

And this are the packages which I use (maybe not all in this code however):

library(reshape)
library(scales)
library(ggplot2)
library(dplyr)
library(tidyr)

Solution

  • Based on the answer by Tung I amended the code for the box plot in the following way:

    ggplot(data = n, aes(y = value, x = as.numeric(variable))) +
    stat_summary(fun.data = min.mean.sd.max, geom = "boxplot", col = "#323232", fill = "#EFC76C") + 
    scale_fill_identity() + 
    scale_x_continuous(breaks = as.numeric(unique(n$variable)), minor_breaks = NULL,
                     labels = "", expand = c(0.12, 0.12)) + 
    scale_y_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7)) +
    expand_limits(y = c(1, 7)) + coord_flip(clip = "off") + labs(x = "", y = "") +
    theme(text = element_text(size = 18), panel.background = element_rect(fill = "#EAEDED"), 
        panel.border = element_rect(fill=NA, color = "grey", size = 0.5, linetype = "solid")) +
    geom_text(x = 1, y = 0.5, inherit.aes = FALSE, label = "Servant Leadership", check_overlap = TRUE, hjust = 1, 
            fontface = 'plain', size = 6, color = "#4E4E4E") +
    theme(plot.margin=unit(c(0.05, 4.5, 0, 9.5),"cm"))
    

    Variable name is added and the plot area stays the same, does not change with the length of the name