繁体   English   中英

如何更改连续日期轴的刻度标签?

[英]How to change tick labels of continuous date axis?

我的图使用Issue.Date作为x轴。 每个Issue.Date应该代表其各自年份的特定季度。 例如,2016-03-31是2016年第一季度,而2016-12-31是2016年第四季度。因此,我想将这些Issue.Date在我的图表上重新标记为“ QX 20XX”。 该数据集将更改,因此我想动态地重新标记而不是硬代码。

这是我正在使用的样本数据集:

   Issue.Date NumberofLoans Outstanding.Principal Actual.Loss     Amount Internal.Score    Bad.Rate
1  2016-03-31           198             355142.61    22652.04  894971.10              A  114.404242
2  2016-06-30           351             864540.92    56865.36 1778844.16              A  162.009573
3  2016-09-30           410            1341855.67    43443.02 1982258.88              A  105.958585
4  2016-12-31           447            1773268.64    12830.79 2059398.33              A   28.704228
5  2017-03-31           588            2500605.62     8096.68 2606753.91              A   13.769864
6  2016-03-31           222             221366.61    33524.42  705043.74              B  151.010901
7  2016-06-30           290             512027.61    51499.88 1091120.09              B  177.585793
8  2016-09-30           366             897087.22    47512.03 1406191.21              B  129.814290
9  2016-12-31           394            1236908.78     9345.20 1462646.78              B   23.718782
10 2017-03-31           856            2745057.44     1339.81 2972873.39              B    1.565199
11 2016-03-31           108              64708.88    21563.01  244001.02              C  199.657500
12 2016-06-30           164             156459.74    36033.51  443153.61              C  219.716524
13 2016-09-30           143             207445.31    24388.17  409115.01              C  170.546643
14 2016-12-31           174             443051.98     1288.02  571596.79              C    7.402414
15 2017-03-31           209             515509.21        0.00  568733.95              C    0.000000
16 2016-03-31             7              10565.30      623.77   23359.92              D   89.110000
17 2016-06-30             4               8748.47     4924.43   14400.06              D 1231.107500
18 2016-09-30             3              10115.38        0.00   12237.75              D    0.000000
19 2016-12-31             4               7053.01        0.00   11356.39              D    0.000000
20 2017-03-31             9              19184.60        0.00   24762.99              D    0.000000

使用此代码...

data = read.csv("C:/Users/Riley/OneDrive/Documents/KPI Report/example.csv")
data$Issue.Date = as.Date(data$Issue.Date, "%m/%d/%Y")
print(data)


# build plot 1 (Loan Amount)
p1 <- ggplot(data, aes(y=Amount, x=Issue.Date)) + 
  geom_point(color='red') + 
  scale_y_continuous(labels = scales::dollar) +
  facet_wrap(~Internal.Score)  +
  theme_bw() %+replace% theme(panel.background = element_rect(fill = NA)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

# build plot 2 (# of Loans)
p2 <- ggplot(data, aes(y=NumberofLoans, x=Issue.Date)) +
  geom_point(color='green') + facet_wrap(~Internal.Score) + theme_bw() %+replace%
  theme(panel.background = element_rect(fill = NA)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

# extract gtable
g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))

# overlap panels of plots
pp <- c(subset(g1$layout, grepl("panel",name), se = t:r))
g <- gtable_add_grob(g1, g2$grobs[grep("panel", g2$layout$name)], pp$t, pp$l, pp$b, pp$l)

# axis tweaks
ia <- which(grepl("axis_l", g2$layout$name) | grepl("axis-l", g2$layout$name))
ga <- g2$grobs[ia]

axis_idx <- as.numeric(which(sapply(ga,function(x) !is.null(x$children$axis))))

for(i in 1:length(axis_idx)){
  ax <- ga[[axis_idx[i]]]$children$axis
  ax$widths <- rev(ax$widths)
  ax$grobs <- rev(ax$grobs)
  ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
  g <- gtable_add_cols(g, g2$widths[g2$layout[ia[axis_idx[i]], ]$l], length(g$widths) - 1)
  g <- gtable_add_grob(g, ax, pp$t[axis_idx[i]], length(g$widths) - i, pp$b[axis_idx[i]])
}

# Plot
grid.newpage()
grid.draw(g)

我得到以下图:

在此处输入图片说明

使用zoo包中的as.yearqtr函数,可以将class date的变量转换为yearqtr类。 然后,您可以使用此软件包中的scale_x_yearqtr函数缩放x轴。

我首先稍微简化了您的最小可重现示例,然后发现了一些对我有用的东西:

data$Issue.Date <- zoo::as.yearqtr(as.Date(data$Issue.Date))

ggplot(data, aes(y=Amount, x=Issue.Date)) + 
  geom_point(color='red') + 
  scale_y_continuous(labels = scales::dollar) +
  zoo::scale_x_yearqtr(format = "Q%q-%Y", expand = c(0, 0))+
  facet_wrap(~Internal.Score)  +
  theme_bw() %+replace% theme(panel.background = element_rect(fill = NA)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

要生成此图:

在此处输入图片说明

根据我的经验, zoo包并不总是与ggplot2 / tidyverse配合使用,因此我通常避免将它们组合在一起。 例如,如果我不在x轴的比例函数内添加expand = c(0, 0)在2015年第4季度添加一个勾号。如果您想在x轴的侧面留出更多空间,可以通过增加扩展值并为x轴设置明确的限制来找到解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM