sa90210
sa90210

Reputation: 585

How to transpose legend in ggplot2

I have the following figure created using ggplot2. I'm looking for a way to transpose the legend (so that the dates are vertical as opposed to horizontal)? Is there any way to do this?

library(ggplot2)

yieldcurve_sa <- structure(list(Date = structure(c(18629, 18656, 18629, 18656, 
18629, 18656, 18629, 18656, 18629, 18656, 18629, 18656, 18629, 
18656), class = "Date"), Key = structure(c(1L, 1L, 2L, 2L, 3L, 
3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L), .Label = c("3M", "2Y", "5Y", 
"10Y", "20Y", "25Y", "30Y"), class = "factor"), Value = c(2.89, 
4.92, 4.53, 4.655, 6.665, 6.685, 8.735, 8.735, 10.845, 10.825, 
10.905, 10.885, 10.81, 10.8)), row.names = c(NA, -14L), class = "data.frame")

ggplot(yieldcurve_sa, aes(x = Key, y = Value, col = factor(Date), group=factor(Date))) +
  geom_line(size = 1.2) +
  scale_color_manual(values = c('#7c3042','#c7af76')) +
  labs(y="", x = 'Time to Maturity', caption = '*Source - SARB') +
  theme_bw() +
  theme(legend.title = element_blank(),legend.position = 'left',
        legend.margin=margin(0,0,0,0),
        legend.box.margin=margin(-10,-10,-10,-10)) +
  guides(color = guide_legend(label.theme = element_text(angle = -90),override.aes = list(size = 3))) +
  scale_y_continuous(labels = function(x) paste0(x, "%"))

EDIT: I've rotated my legend, just need to force a space between the two entries so that the dates can be distinguished from each other.

enter image description here

Upvotes: 2

Views: 232

Answers (2)

Jon Spring
Jon Spring

Reputation: 66570

Alternatively, add some padding to the series names:

ggplot(yieldcurve_sa, aes(x = Key, y = Value, 
                          col = factor(Date) %>% str_pad(14, "both"), 
                          group=factor(Date))) +

enter image description here

Upvotes: 1

jared_mamrot
jared_mamrot

Reputation: 26650

You can transpose the legend using this solution, then increase the spacing between legend labels by adjusting legend.key.height, e.g.

library(tidyverse)

yieldcurve_sa <- structure(list(Date = structure(c(18629, 18656, 18629, 18656, 
                                                   18629, 18656, 18629, 18656, 18629, 18656, 18629, 18656, 18629, 
                                                   18656), class = "Date"), Key = structure(c(1L, 1L, 2L, 2L, 3L, 
                                                                                              3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L), .Label = c("3M", "2Y", "5Y", 
                                                                                                                                              "10Y", "20Y", "25Y", "30Y"), class = "factor"), Value = c(2.89, 
                                                                                                                                                                                                        4.92, 4.53, 4.655, 6.665, 6.685, 8.735, 8.735, 10.845, 10.825, 
                                                                                                                                                                                                        10.905, 10.885, 10.81, 10.8)), row.names = c(NA, -14L), class = "data.frame")

ggplot(yieldcurve_sa, aes(x = Key, y = Value, col = factor(Date), group=factor(Date))) +
  geom_line(size = 1.2) +
  scale_color_manual(values = c('#7c3042','#c7af76')) +
  labs(y="", x = 'Time to Maturity', caption = '*Source - SARB') +
  theme_bw() +
  theme(legend.title = element_blank(),
        legend.position = 'left',
        legend.margin=margin(0,0,0,0),
        legend.key.height = unit(2.6, "cm")) +
  guides(color = guide_legend(label.theme = element_text(angle = -90),
                              override.aes = list(size = 3))) +
  scale_y_continuous(labels = function(x) paste0(x, "%"))

example_1.png

Upvotes: 1

Related Questions