2017-10-05 132 views
0

我有從時間幀02.01.2007-30.12.2016,它由兩個率和這些精確率的差以下數據集:繪製在y軸上的多個變量隨時間

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 

我想要在x軸上繪製日期以便在x軸上顯示每年的結束時間:2007-2016。

另外還有三個其他變量,它們在y軸上用基點表示,線條通過數據點。有不同的線條樣式會很好,例如一個實線,一個虛線和一個虛線。 最後,我想在圖上顯示變量名稱的圖例。

或多或少就像這個例子只是不電網:

plot example

回答

1

簡單而快速的地塊使用ggplot2

library(ggplot2) 
library(reshape2) 
d <- melt(risk_free_rate_comparison, "Dates") 
d$Dates <- as.Date(d$Dates) 
ggplot(d, aes(Dates, value, color = variable, linetype = variable)) + 
    geom_line() + 
    labs(color = NULL, linetype = NULL) + 
    theme_classic() + 
    theme(legend.position = "bottom") 

reshape2::melt變換(分組您的數據)。在這種情況下按Date您想放在x軸上。 theme_classic()ggplot2沒有網格的主題。

enter image description here

+0

完美,謝謝 – rbonac

+0

對不起一個問題,就是它可以去除字變量中的傳奇人物,情節下移動的傳奇? – rbonac

+1

@rbonac我編輯了我的答案,現在應該沒問題 – PoGibas

相關問題