[英]Independent two Y-axes
我的目標是 plot 日期與使用兩個獨立 y 軸的一張圖中的金額和金額百分比變化進行對比。 這是 csv 文件中名為 DerivativeMeasure 的數據
Date Notional
30/06/1998 72106521.77
31/12/1998 80276622.05
30/06/1999 81420274.61
31/12/1999 88156431.71
30/06/2000 93959822.42
31/12/2000 95150854.68
30/06/2001 99648589.78
31/12/2001 111058769.9
30/06/2002 127372621.6
31/12/2002 141513417.2
我使用公式Notional_change= ([ Notional(n) - Notional(n-1)]/ Notional(n-1))*100
來計算百分比變化,我的目標是 plot Notional
和Notional_change
針對Date
,但是Notional_change
長度比日期少1,我怎么plot這個? 這是完整的代碼
library("ggplot2")
library("scales")
library(tidyverse)
OTCData <- read.csv("~/r programs/Research/DerivativeMeasure.csv")
OTCData <- mutate(OTCData, Date = as.Date(OTCData$Date, "%d/%m/%Y"))
Notional_amount <-OTCData[,"Notional", drop=FALSE]
#rownames(Notional_amount) <- OTCData$Date
n <-nrow(Notional_amount)
Notional_change <-(((Notional_amount[2:n, 1]-Notional_amount[1:(n-1),1])/Notional_amount[1:(n-1),1])*100)
class(Notional_change)
names(Notional_change) <- OTCData[2:n, 1]
head(Notional_change)
# get the rescaling factor from the max of the numerical column and the max percent
n_max <- max(OTCData$Notional, na.rm = TRUE)
p_max <- max(Notional_change, na.rm = TRUE)
scaling_factor <- p_max / n_max
ggplot(OTCData, aes(x = OTCData$Date)) +
geom_line(aes(y = Notional, color="red"), group =1) +
geom_line(aes(y = Notional_change / scaling_factor, colour = "blue"), group =1) +
scale_colour_identity(name = NULL, breaks = c("red", "blue"), labels = c("Notional", "Percent"), guide = "legend") +
scale_y_continuous(
name = "Notional Axis",
sec.axis = ggplot2::sec_axis(~ . * scaling_factor,
name = "Percent Axis",
labels = scales::percent_format(accuracy = 1))
)
我得到的錯誤,我猜這與 Notional_change 的長度有關,在這里
錯誤:美學必須是長度 1 或與數據 (44) 相同:y
請幫忙
沒錯,問題是OTCData
和Nominal_change
中的行數不匹配。 這可以通過在Nominal_change
中添加一個虛擬的第一行來解決(這是通過下面的bind_rows()
行完成的)。 代碼和output如下圖所示:
library(tidyverse)
OTCData <- tibble(Date = c("30/06/1998", "31/12/1998", "30/06/1999", "31/12/1999",
"30/06/2000", "31/12/2000", "30/06/2001", "31/12/2001",
"30/06/2002", "31/12/2002"),
Notional = c(72106521.77, 80276622.05, 81420274.61, 88156431.71,
93959822.42, 95150854.68, 99648589.78, 111058769.9,
127372621.6, 141513417.2))
OTCData <- mutate(OTCData, Date = as.Date(OTCData$Date, "%d/%m/%Y"))[![enter image description here][1]][1]
Notional_amount <-OTCData[,"Notional", drop=FALSE]
#rownames(Notional_amount) <- OTCData$Date
n <-nrow(Notional_amount)
Notional_change <-(((Notional_amount[2:n, 1]-Notional_amount[1:(n-1),1])/Notional_amount[1:(n-1),1])*100)
class(Notional_change)
names(Notional_change) <- "Notional_change"
head(Notional_change)
Notional_change <- bind_rows(tibble(Notional_change = NA), Notional_change)
# get the rescaling factor from the max of the numerical column and the max percent
n_max <- max(OTCData$Notional, na.rm = TRUE)
p_max <- max(Notional_change, na.rm = TRUE)
scaling_factor <- p_max / n_max
OTCData <- OTCData %>% mutate(Notional_change = Notional_change$Notional_change)
ggplot(OTCData, aes(x = Date)) +
geom_line(aes(y = Notional, color="red"), group =1) +
geom_line(aes(y = Notional_change / scaling_factor, colour = "blue"), group =1) +
scale_colour_identity(name = NULL, breaks = c("red", "blue"), labels = c("Notional", "Percent"), guide = "legend") +
scale_y_continuous(
name = "Notional Axis",
sec.axis = ggplot2::sec_axis(~ . * scaling_factor,
name = "Percent Axis",
labels = scales::percent_format(accuracy = 1))
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.