2017-10-05 71 views
0

嘿我是新来的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") 
+ theme(
+ legend.position = "bottom", 
+ legend.direction = "vertical", 
+ legend.box.margin = margin(t = 20), 
+ axis.title.x = element_text(margin = margin(t = 20)), 
+ axis.title.y = element_text(margin = margin(r = 20)) 
+) 

riskfreeratescomparison

现在我想改变黑色和白色风格的三个变量的线条颜色。为了能够区分线条,也许可以改变线条粗细或以某种方式进行。

+0

您可以使用'scale_colour_manual()','scale_linetype_manual()','scale_size_manual()'手动更改线条颜色,线宽和线型。你没有提供一个可重复的例子,所以看看例如例如'?scale_colour_manual'。 –

回答

1

@rbonac手动更改线的颜色和大小所需的值,看看是否可以帮助你,感觉通过注释不同的图层或使用scale_line_manual()手动设置颜色来自由地进行实验。

library(tidyverse) 

mtcars %>% 
    mutate(cyl = factor(cyl, labels = c("4", "6", "8"))) %>% 
    ggplot(., aes(wt, mpg, color = cyl, linetype = cyl)) + 
    geom_line(size = 2) + 
    labs(color = NULL, linetype = NULL) + 
    theme_classic() + 
    ylab("Rates in bp") + 
    scale_linetype_discrete() + 
    scale_color_grey() + 
    theme(
    legend.position = "bottom", 
    legend.direction = "vertical", 
    legend.box.margin = margin(t = 30), 
    axis.title.x = element_text(margin = margin(t = 20)) 
) 
1

可以将每个变量分配到线使用尺寸审美宽度,使用scale_colour_manualscale_size_manual

ggplot(d, aes(Dates, value, color = variable)) + 
    geom_line(aes(linetype = variable, size=variable)) + 
    labs(color = NULL, linetype = NULL, size = NULL) + 
    theme_classic() + 
    theme(legend.position = "bottom") + 
    ylab("Rates in bp") + 
    scale_colour_manual(values=c("gray50", "gray25", "gray0")) + 
    scale_size_manual(values=c(1, 1.5, 2)) + 
    theme(
    legend.position = "bottom", 
    legend.direction = "vertical", 
    legend.box.margin = margin(t = 20), 
    axis.title.x = element_text(margin = margin(t = 20)), 
    axis.title.y = element_text(margin = margin(r = 20)) 
    ) 
相关问题