繁体   English   中英

如何在 ggplot2 中转置图例

[英]How to transpose legend in ggplot2

我使用ggplot2创建了下图。 我正在寻找一种转换图例的方法(以便日期是垂直的而不是水平的)? 有没有办法做到这一点?

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, "%"))

编辑:我已经旋转了我的图例,只需要在两个条目之间强制一个空格,以便可以区分日期。

在此处输入图像描述

您可以使用此解决方案转置图例,然后通过调整legend.key.height来增加图例标签之间的间距,例如

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, "%"))

示例_1.png

或者,为系列名称添加一些填充:

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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM