7

With ggplot, I want to add a left aligned legend title with multiple lines and exponents in the text for the units of the values in the legend. I'm plotting data of a form similar to:

leakage_rates_levels <- c(5.4, 0.25)
leakage_rates <- as.factor(rep(leakage_rates_levels, 3))  # L/s-m^2 at 75 Pa
data_groups_levels <- c('Set 1', 'Set 2', 'Set 3')
data_groups <- as.factor(rep(data_groups_levels, each=2))
moisture_level <- c(7, 3, 11, 10, 16, 6)
plotdt <- data.frame(data_groups, leakage_rates, moisture_level)

I use expression() to add exponents to the units in the legend. The following code generates the desired figure, but with the legend title text mis-formatted.

ggplot(plotdt, aes(data_groups)) +
  geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
  labs(y='Moisture Level') +
  labs(fill=expression(paste('Leakage Rate\nat 75 Pa\n(L/s-', m^2, ')', sep=''))) +
  theme(panel.grid.major.x = element_blank(),
        axis.title.x = element_blank())

The legend title appears left aligned except for the final line, which has a bunch of extraneous spaces in the middle of it.

Three line legend text where the third line indicates the units, liters per second meter squared, but the meter squared term is pushed to the right margin while all the other text is left aligned, leaving a large white space in the middle of the third line.

Using legend_title_align=0 (suggested here) and/or legend_title=element_text(hjust=1) in theme() have no effect. Trying to add phantom() spacing also did not work (suggested here). The end of the top answer to this question notes the same problem I'm encountering but does not propose a solution.

Is there a way to get the meter squared term in the legend to be left-aligned like the rest of the text?

I am using ggplot 3.1.0 and R 3.5.1.

trynthink
  • 301
  • 5
  • 13

2 Answers2

7

You can use the unicode representation of superscript two (U+00B2) and avoid the problem-causing combination of expression() and a multi-line legend title:

ggplot(plotdt, aes(data_groups)) +
  geom_bar(aes(weight=moisture_level, fill=leakage_rates), position='dodge') +
  labs(y='Moisture Level') +
  labs(fill=paste('Leakage Rate\nat 75 Pa\n(L/s-m\u00b2)', sep='')) +
  theme(panel.grid.major.x = element_blank(),
        axis.title.x = element_blank())

Plot using Superscript

yrx1702
  • 1,619
  • 15
  • 27
2

You can use atop to have lines "atop" each other.

Because you have 3 lines and atop only accepts 2 arguments however, you need to have 2 atop nested in one another. This makes the font on some of the lines smaller. The way to prevent this is to pass the expressions to either textstyle or displaystyle:

ggplot(plotdt, aes(data_groups)) +
  geom_bar(aes(weight = moisture_level, fill = leakage_rates), position = "dodge") +
  labs(y = "Moisture Level") +
  labs(fill = expression(atop(atop(textstyle("Leakage Rate"),
                                   textstyle("at 75 Pa")),
                              "(L/s-" ~m^2~ ")"))) +
  theme(panel.grid.major.x = element_blank(), axis.title.x = element_blank())

enter image description here

prosoitos
  • 6,679
  • 5
  • 27
  • 41