R ggplot季度数据高亮表已取代数字

时间:2012-02-04 15:17:08

标签: r ggplot2

我正在使用geom_tile来创建一个包含在数据框中的季度数字表。这似乎没有问题,除非数据系列在年中开始,通常是在4月到6月的季度。如果我在日历年的开头绘制数据框,那就没有问题:我得到了一个结构良好的高亮表。

from start of CY

然而,要实现这一点,我也排除了一些数据。如果我在一年中指定一个开始日期,那么数据会被分散/混乱,如下所示:

enter image description here

Q值。如何绘制表格,即使系列在年中开始,它也会以某种方式填充它开始的年份的任何缺失数据?也就是说,如果我的数据在7月到9月的季度开始,我如何强制ggplot将第一个数据点放在第三列中,在左边留下两个空白区块?

Q值。作为后续问题或者以不同方式提出的相同问题,我如何指定第一列中应该包含哪个季度?理想情况下,我希望能够指明每年的7月至9月季度位于表格的第一列,而1月至3月的季度则位于第四列。

示例如下:

require(lubridate)
require(ggplot2)

set.seed(12345)
df <- data.frame(date=seq(as.Date("2003/06/01"), by="month", length.out=103),myval=runif(103, min=-1, max=1))
df$date <- (df$date + months(1)) - days(1) # get last day of month
df$year <- as.numeric(format(as.Date(df$date), format="%Y"))
df$month <- as.numeric(format(as.Date(df$date), format="%m"))
# create quarterly label
df$qtr <- ifelse(df$month==3,"Jan-Mar",ifelse(df$month==6,"Apr-Jun",ifelse(df$month==9,"Jul-Sep",ifelse(df$month==12,"Oct-Dec",""))))
qtr <- df[ df$month %in% c(3,6,9,12), ] # extract quarter-end figures

p <- ggplot(qtr[qtr$date>='2004-01-01',], aes(x=qtr,y=year(date), fill = myval, label = sprintf("%1.1f%%", 100*myval))) + 
  scale_y_date(major="years", format="%Y") +
  scale_y_reverse(breaks=2003:2012, labels=2003:2012, expand=c(0,0)) + 
  geom_tile() + geom_text(size=geomtextsize,colour = "black") +
  scale_fill_gradient2(low = "blue", high = "red",,midpoint=0) +
  scale_x_discrete(expand=c(0,0))

print(p)

修改 显示包含Vincent建议的最终改进版本的图像以及用于生成它的代码如下所示。

final version of highlight table

set.seed(12345)
df <- data.frame(date=seq(as.Date("2003/06/01"), by="month", length.out=103),myval=runif(103, min=-1, max=1))
df$date <- (df$date + months(1)) - days(1) # get last day of month
df$year <- as.numeric(format(as.Date(df$date), format="%Y"))
df$month <- as.numeric(format(as.Date(df$date), format="%m"))
# create quarterly label
df$qtr <- ifelse(df$month==3,"Jan-Mar",ifelse(df$month==6,"Apr-Jun",ifelse(df$month==9,"Jul-Sep",ifelse(df$month==12,"Oct-Dec",""))))
df$qtr[ df$qtr=="" ] <- NA
df$display_year <- ifelse( df$month < 4, df$year - 1, df$year )
df$display_year <- paste( df$display_year, df$display_year + 1, sep="-" )
df$qtr <- ordered(df$qtr, levels=c("Apr-Jun", "Jul-Sep", "Oct-Dec", "Jan-Mar"))
qtr <- df[ df$month %in% c(3,6,9,12), ]
qtr$display_year <- factor( qtr$display_year, levels = sort( unique(qtr$display_year), decreasing=TRUE ) )

p <- ggplot(qtr, aes(x=qtr,y=display_year, fill = myval, label = sprintf("%1.1f%%", 100*myval))) + 
scale_y_discrete(expand=c(0,0)) +
geom_tile() + geom_text(size=geomtextsize,colour = "black") +
scale_fill_gradient2(low = "blue", high = "red",,midpoint=0) +
scale_x_discrete(expand=c(0,0))
p

1 个答案:

答案 0 :(得分:3)

您可以通过确保类型为ordered来强制执行季度订单。

df$qtr[ df$qtr=="" ] <- NA
df$qtr <- ordered(df$qtr, 
  levels=c("Jan-Mar", "Apr-Jun", "Jul-Sep", "Oct-Dec"))

对于第二个问题, 您还需要将年份列更改为4月到3月的年份。

df$display_year <- ifelse( df$month < 4, df$year - 1, df$year )
df$display_year <- paste( df$display_year, df$display_year + 1, sep="-" )
# Check the data
df[,c("year", "month", "qtr", "display_year")]
# Small change in the plot: the y axis is no longer a date
df$qtr <- ordered(df$qtr, levels=c("Apr-Jun", "Jul-Sep", "Oct-Dec", "Jan-Mar"))
qtr <- df[ df$month %in% c(3,6,9,12), ]
p <- ggplot(qtr[qtr$date>='2004-01-01',], aes(x=qtr,y=display_year, fill = myval, label = sprintf("%1.1f%%", 100*myval))) + 
  scale_y_discrete(expand=c(0,0)) +
  geom_tile() + geom_text(size=geomtextsize,colour = "black") +
  scale_fill_gradient2(low = "blue", high = "red",,midpoint=0) +
  scale_x_discrete(expand=c(0,0))
p