Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7405749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:27:03+00:00 2026-05-29T05:27:03+00:00

I am using geom_tile to create a table of quarterly figures, which is contained

  • 0

I am using geom_tile to create a table of quarterly figures, which is contained in a data frame. This seems to work without problems except when the data series begins in the middle of the year, typically in the April to June quarter. If I plot the data frame at the beginning of the calendar year, there is no problem: I get a well-formed highlight table.

from start of CY

However, to achieve that I have also excluded some of my data. If I specify a start date part way through the year, the data gets dispaced/jumbled up, like so:

enter image description here

Q. How can I plot the table such that even when the series begins in mid-year it somehow pads any missing data for the year in which it starts? That is, if my data begins in the July to September quarter, how can I force ggplot to put that first data point in the third column, leaving two blank tiles to the left?

Q. As a follow-up or perhaps the same question asked in a different way, how can I specify which quarter should be in the first column? Ideally I would like to be able to specify that the Jul-Sep quarter of each year is in the first column of the table and that the January to March quarter is in the fourth column.

Example follows:

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)

EDIT:
An image showing the final improved version incorporating Vincent’s suggestions and the code used to generate it follow below.

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:27:04+00:00Added an answer on May 29, 2026 at 5:27 am

    You can enforce the order of the quarters by ensuring they are of type ordered.

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

    For the second question,
    you also need to change the year column to be the April-to-March year.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame (which I melted using the melt function), from which I
I have the following part of a data frame which is much bigger than
I have a data frame for which I'm computing a linear model and would
i am using rpy2-2.0.7 (i need this to work with windows 7, and compiling
I produced multiple plots using the following code: set.seed(12345) a <- data.frame(Glabel=LETTERS[1:7], A=rnorm(7, mean
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
I want to create a scatter plot with regression line, while using size aesthetics
I calculated following data frame of maximimum response time of a server with pre-defined
I'm plotting some data using geom_smooth and looking for a way to change the
I am using directlabels for the first time today and as this is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.