[英]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.