繁体   English   中英

在R中的ggplot中重新定位图例条目和轴标签

[英]Relocate legend entries and axis labels in ggplot in R

嘿,我是R的新手,有一个非常小的问题

我有以下数据集:

head(risk_free_rate_comparison)

   Dates `       Swap rate` `Sovereign bond yield rate` `Swap rate - Sovereign bond yield rate`
  <dttm>         <dbl>       <dbl>                       <dbl>
1 2007-01-02     408.9       380.9568                    27.9432
2 2007-01-03     410.3       380.4535                    29.8465
3 2007-01-04     409.2       381.3993                    27.8007
4 2007-01-05     414.3       385.0663                    29.2337
5 2007-01-08     413.1       384.2545                    28.8455
6 2007-01-09     415.5       384.9770                    30.5230

,如下图:

ggplot(d, aes(Dates, value, color = variable, linetype = variable)) +
+     geom_line() +
+     labs(color = NULL, linetype = NULL) +
+     theme_classic() +
+     theme(legend.position = "bottom") +
+     ylab("Rates in bp")

riskfreeratecomparison

我对图例的位置感到满意,但是是否可以使条目一个位于另一个的下方而不是并排放置?

此外,出于美观原因,我想将轴标签移离轴一点点。

最后添加我的theme()可以解决您的两个问题:

library(tidyverse)

mtcars %>% 
  mutate(am = factor(am, labels = c("auto", "manu"))) %>% 
  ggplot(., aes(wt, mpg, color = am, linetype = am)) +
  geom_line() +
  labs(color = NULL, linetype = NULL) +
  theme_classic() +
  ylab("Rates in bp") + 
  theme(
    legend.position = "bottom",
    legend.direction = "vertical",
    legend.box.margin = margin(t = 30),
    axis.title.x = element_text(margin = margin(t = 20))
  )

暂无
暂无

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

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