嘿,我是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))
+ )
现在我想将三个变量的线条颜色更改为黑白样式。并且可能以这种方式改变线条厚度或某些东西,以便能够区分线条。
答案 0 :(得分:1)
您可以使用尺寸美学将每个变量指定为线宽,并使用scale_colour_manual
和scale_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))
)
答案 1 :(得分: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))
)