How to adjust labels of facet strip in ggplot2

Viewed 1043

I tried a new package ggthemr, but I've encountered two problems.

The dataset is:

tma<-data.frame(
invasion=c(0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1),
sediment=c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1),
nitrogen=c( 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1),
type=c("Evenness index",
"Evenness index",
"Evenness index",
"Evenness index",
"Total mass(g)",
"Total mass(g)",
"Total mass(g)",
"Total mass(g)",
"Evenness index",
"Evenness index",
"Evenness index",
"Evenness index",
"Total mass(g)",
"Total mass(g)",
"Total mass(g)",
"Total mass(g)"),
mean=c(0.76109,
0.63923,
0.78138,
0.73626,
59.0425,
56.15383,
50.39167,
44.9215,
0.60109,
0.55402,
0.81587,
0.74957,
62.207,
53.21033,
49.99517,
42.38767),
standard.error=c(0.03638,
0.06232,
0.045,
0.03912,
5.87524,
5.87524,
5.87524,
5.87524,
0.05093,
0.04807,
0.02694,
0.04843,
5.87524,
5.87524,
5.87524,
5.87524)
)

tma$nitrogen <- as.factor(tma$nitrogen)
tma$sediment <- as.factor(tma$sediment)

The code is:

library(tidyverse)
library(ggthemr)

ggthemr('light', layout = 'scientific',
    spacing = 1, type = 'inner', line_weight = 0.6,
    )

ggplot(tma, aes(invasion, mean, color = sediment)) +
geom_ribbon(
    aes(ymin = mean - standard.error, ymax = mean + standard.error,group=sediment),
    alpha = 0.2,
    color = NA
    ) +
geom_line(size = 1) +
geom_point(show.legend = TRUE) +
facet_grid(type ~ nitrogen, scales = "free_y") +
scale_x_discrete(breaks = NULL, expand = c(0, 0.10)) +
scale_y_continuous(sec.axis = dup_axis(name=NULL))+
theme(legend.position = "bottom") +
theme(panel.spacing.x = unit(0.1, "lines"), panel.spacing.y = unit(0.1, "lines")) +
theme(strip.text.x = element_text(size = 10),
    strip.text.y = element_text(size = 14),
    axis.title.y = element_text(size = 14),
    axis.title.x = element_text(size = 14),
    ) +
xlab("From not invaded to invaded") +
ylab("Mean with standard error")

The output is like this:

enter image description here

I expected that the duplicated y-axis is on the right of the plot, and on the left of the facet strip, but it is actually on the right of facet strip.

Another problem is that the facet strip cannot be displayed completely, for example, the title of Total mass (g).

What's going wrong? I don't know what parameters should I adjust. Thanks a lot.

1 Answers
library(devtools)    
# devtools::install_github('cttobin/ggthemr') # need this to install ggthemr
library(ggthemr)

ggthemr('light', layout = 'scientific',
        spacing = 1, type = 'inner', line_weight = 0.6
)

ggplot(tma, aes(invasion, mean, color = sediment)) +
  geom_ribbon(
    aes(ymin = mean - standard.error, 
        ymax = mean + standard.error,group = sediment), 
    alpha = 0.2, color = NA) +
  geom_line(size = 1) +
  geom_point(show.legend = TRUE) +
  facet_grid(type ~ nitrogen, scales = "free_y") +
  scale_x_discrete(breaks = NULL, expand = c(0, 0.10)) +
  scale_y_continuous(sec.axis = dup_axis(name=NULL))+
  theme(legend.position = "bottom",
        panel.spacing.x = unit(0.1, "lines"), 
        panel.spacing.y = unit(0.1, "lines"),
        strip.text.x = element_text(size = 10),
        strip.text.y = element_text(size = 14, margin = margin(0,0,0,5)),
        strip.placement = "outside",
        axis.title.y = element_text(size = 14),
        axis.title.x = element_text(size = 14)) +
  xlab("From not invaded to invaded") +
  ylab("Mean with standard error")

Notes:

  1. To fix your "facet label on left side of y-axis" issue, I added theme(strip.placement = "outside"). This puts the strip on the other side of the axes.
  2. To fix the "labels not fully displayed" issue, I added a margin = margin(0,0,0,5) argument in element_text of strip.text.y. This controls the margins of the strip text. At first, it seems confusing as to which number to adjust, but a quick ?element_text tells me that the order is: t = top, r = right, b = bottom, l = left (think "trouble"). So the last number controls the left margin of the strip text.
  3. I've also formatted your code to make it a bit cleaner. For example combining all the theme's

enter image description here

Related