David Moore
David Moore

Reputation: 968

Having Two or More Line Breaks and Italicized Words in Axis Labels in a 'ggplot()' Plot in R

I have the following data:

df <- structure(list(Site = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 4L, 
                                    4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
                                    1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Permafrost", "Palsa", 
                                                                                "Palsa Hollow", "Rich Sphagnum Lawn", "Tall Graminoid Fen"), class = "factor"), 
                 Depth = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
                                     2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 
                                     2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Upper Depth", "Lower Depth"
                                     ), class = "factor"), ug.Al.m2 = c(0.093375394, 0.15684537, 
                                                                        0.025747986, 0.031130205, 0.074247144, 0.054740061, 0.006671475, 
                                                                        0.002208133, 0.003427595, 0.001447068, 0.013960114, 0.008988422, 
                                                                        0.047630561, 0.005434406, 0.041627689, 0.004127627, 0.013713378, 
                                                                        0.00501951, 0.512382579, 0.628336756, 0.293063584, 0.460299194, 
                                                                        0.188002926, 0.385744659, 0.220549738, 0.003135834, 0.006755556, 
                                                                        0.012846966, 0.008662843, 0.0064347, 0.004951768)), row.names = c(NA, 
                                                                                                                                          -31L), class = "data.frame")

I am using it to make a barplot:

library (cowplot)
library (ggplot2)
library (RColorBrewer)
X_Axis_Labels <-  c("Permafrost", "Palsa", expression(atop("Palsa", "Hollows")), expression(atop("Rich", italic("Sphagnum"), "Lawn")), expression(atop("Tall", "Graminoid", "Fen")))
Legend_Labels <-  c("Permafrost", "Palsa", "Palsa Hollows", expression(paste("Rich ", italic("Sphagnum"), " Lawn")), "Tall Graminoid Fen")
Palette1 <- c(brewer.pal(11, "RdBu")[c(11,10,9,8,7)])
ggplot(df, aes(x = Site, y = ug.Al.m2, fill = Site)) +
  stat_summary(geom = "bar", width = 0.6,  fun = mean, colour = "black") +
  stat_summary(geom = "errorbar", width = 0.2, fun.data = mean_se) +
  ggtitle("Total Aluminum Concentrations in Permafrost Peatland Communities") +
  scale_x_discrete(labels = X_Axis_Labels) +
  scale_fill_manual(values = Palette1, labels = Legend_Labels) +
  ylab(expression(paste("Aluminum Concentration,  ", mu, "g m" ^ "-2"))) +
  xlab("Site") +
  theme_cowplot(13)

Here's what the graph looks like:

Figure 1

I'm having a lot of trouble getting all three lines of the x axis labels to appear on my graph. The word 'Lawn', which should appear under 'Sphagnum', is lost. Since the word 'Sphagnum' needs to be italicized, I can't simply use the standard line break (\n). I've also tried playing with the plot margins to no avail.

Is there a solution to this problem?

Thank you!

Upvotes: 0

Views: 235

Answers (1)

Duck
Duck

Reputation: 39605

Try this approach with ggtext and element_markdown(). You can use ** for italic and <br> for the break line. You can customize at any level you wish. Here the code:

library (cowplot)
library (ggplot2)
library (RColorBrewer)
library(ggtext)
X_Axis_Labels <-  c("Permafrost", "Palsa", "Palsa<br>Hollows", "Rich<br>*Sphagnum*<br>Lawn",
"Tall<br>*Graminoid*<br>Fen")
Legend_Labels <-  c("Permafrost", "Palsa", "Palsa Hollows", expression(paste("Rich ", italic("Sphagnum"), " Lawn")), "Tall Graminoid Fen")
Palette1 <- c(brewer.pal(11, "RdBu")[c(11,10,9,8,7)])
ggplot(df, aes(x = Site, y = ug.Al.m2, fill = Site)) +
  stat_summary(geom = "bar", width = 0.6,  fun = mean, colour = "black") +
  stat_summary(geom = "errorbar", width = 0.2, fun.data = mean_se) +
  ggtitle("Total Aluminum Concentrations in Permafrost Peatland Communities") +
  scale_x_discrete(labels = X_Axis_Labels) +
  scale_fill_manual(values = Palette1, labels = Legend_Labels) +
  ylab(expression(paste("Aluminum Concentration,  ", mu, "g m" ^ "-2"))) +
  xlab("Site") +
  theme_cowplot(13)+
  theme(axis.text.x = element_markdown())

Output:

enter image description here

Upvotes: 4

Related Questions