在ggplot2中手动缩放离散x轴变量
Manually scale discrete x axis variable in ggplot2
我有一个 ggplot2
图,其中 x 轴变量是一个因素,但代表以月和年为单位的不同时间跨度(1m、3m、6m、1y、2y、3y、5y、7y、10y 、20y 和 30y——这是一个 yield curve)。因子变量给了我很好的标签,但间距均匀。我希望 x 轴间距与时间跨度成正比。
我认为这个调整应该通过 scale_x_discrete()
,但我只看到一个 expand
选项。我唯一的选择是将我的因子转换为连续变量并放弃漂亮的因子标签吗?
这是相关代码。
plotYieldCurve <- ggplot(data=yieldCurveSub, aes(x=variable, y=value, group=Date))
plotYieldCurve <- plotYieldCurve + geom_line(aes(linetype=factor(Date)))
plotYieldCurve <- plotYieldCurve + xlab("Maturity")
plotYieldCurve <- plotYieldCurve + ylab("YTM")
plotYieldCurve <- plotYieldCurve + labs(linetype="Date")
plotYieldCurve
yieldCurveSub
上的dput
如下
> dput(yieldCurveSub)
structure(list(Date = structure(c(14746, 16209, 16574, 14746,
16209, 16574, 14746, 16209, 16574, 14746, 16209, 16574, 14746,
16209, 16574, 14746, 16209, 16574, 14746, 16209, 16574, 14746,
16209, 16574, 14746, 16209, 16574, 14746, 16209, 16574, 14746,
16209, 16574), class = "Date"), variable = structure(c(1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L,
7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L,
11L), .Label = c("1 mo", "3 mo", "6 mo", "1 yr", "2 yr", "3 yr",
"5 yr", "7 yr", "10 yr", "20 yr", "30 yr"), class = "factor"),
value = c(0.16, 0.01, 0.02, 0.16, 0.03, 0.02, 0.23, 0.05,
0.07, 0.36, 0.09, 0.23, 0.81, 0.36, 0.63, 1.3, 0.79, 1.01,
2.2, 1.56, 1.6, 2.9, 2.09, 2.01, 3.47, 2.54, 2.27, 4.17,
3.11, 2.79, 4.35, 3.39, 3.05)), row.names = c(NA, -33L), .Names = c("Date",
"variable", "value"), class = "data.frame")
将它们视为数字是否可以接受?
yieldCurveSub$variable <- ifelse(grepl("mo", yieldCurveSub$variable),
as.numeric(as.character(gsub("[^0-9]", "", yieldCurveSub$variable))) / 12,
as.numeric(as.character(gsub("[^0-9]", "", yieldCurveSub$variable))))
ggplot(yieldCurveSub) +
geom_point(aes(x = variable, y = value)) +
scale_x_continuous(breaks = c(1/12, 3/12, 6/12, 1, 2, 3, 5, 7, 10,20, 30),
labels = c("1 mo", "3 mo", "6 mo", "1 yr", "2 yr", "3 yr", "5 yr", "7 yr",
"10 yr", "20 yr", "30 yr")
我有一个 ggplot2
图,其中 x 轴变量是一个因素,但代表以月和年为单位的不同时间跨度(1m、3m、6m、1y、2y、3y、5y、7y、10y 、20y 和 30y——这是一个 yield curve)。因子变量给了我很好的标签,但间距均匀。我希望 x 轴间距与时间跨度成正比。
我认为这个调整应该通过 scale_x_discrete()
,但我只看到一个 expand
选项。我唯一的选择是将我的因子转换为连续变量并放弃漂亮的因子标签吗?
这是相关代码。
plotYieldCurve <- ggplot(data=yieldCurveSub, aes(x=variable, y=value, group=Date))
plotYieldCurve <- plotYieldCurve + geom_line(aes(linetype=factor(Date)))
plotYieldCurve <- plotYieldCurve + xlab("Maturity")
plotYieldCurve <- plotYieldCurve + ylab("YTM")
plotYieldCurve <- plotYieldCurve + labs(linetype="Date")
plotYieldCurve
yieldCurveSub
上的dput
如下
> dput(yieldCurveSub)
structure(list(Date = structure(c(14746, 16209, 16574, 14746,
16209, 16574, 14746, 16209, 16574, 14746, 16209, 16574, 14746,
16209, 16574, 14746, 16209, 16574, 14746, 16209, 16574, 14746,
16209, 16574, 14746, 16209, 16574, 14746, 16209, 16574, 14746,
16209, 16574), class = "Date"), variable = structure(c(1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L,
7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L,
11L), .Label = c("1 mo", "3 mo", "6 mo", "1 yr", "2 yr", "3 yr",
"5 yr", "7 yr", "10 yr", "20 yr", "30 yr"), class = "factor"),
value = c(0.16, 0.01, 0.02, 0.16, 0.03, 0.02, 0.23, 0.05,
0.07, 0.36, 0.09, 0.23, 0.81, 0.36, 0.63, 1.3, 0.79, 1.01,
2.2, 1.56, 1.6, 2.9, 2.09, 2.01, 3.47, 2.54, 2.27, 4.17,
3.11, 2.79, 4.35, 3.39, 3.05)), row.names = c(NA, -33L), .Names = c("Date",
"variable", "value"), class = "data.frame")
将它们视为数字是否可以接受?
yieldCurveSub$variable <- ifelse(grepl("mo", yieldCurveSub$variable),
as.numeric(as.character(gsub("[^0-9]", "", yieldCurveSub$variable))) / 12,
as.numeric(as.character(gsub("[^0-9]", "", yieldCurveSub$variable))))
ggplot(yieldCurveSub) +
geom_point(aes(x = variable, y = value)) +
scale_x_continuous(breaks = c(1/12, 3/12, 6/12, 1, 2, 3, 5, 7, 10,20, 30),
labels = c("1 mo", "3 mo", "6 mo", "1 yr", "2 yr", "3 yr", "5 yr", "7 yr",
"10 yr", "20 yr", "30 yr")