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 7783065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:44:04+00:00 2026-06-01T19:44:04+00:00

I am trying to replicate Hadley Wickham’s example on page 86 of the ggplot2

  • 0

I am trying to replicate Hadley Wickham’s example on page 86 of the ggplot2 book, where he overlays geom_rects showing the party in power over the trend in unemployment. Here is my core code, with some sample data:

library("ggplot2")
library("reshape2")
library("lubridate")

con <- textConnection("Date,Var1,Var2
8-Jan-12,100.8,116
15-Jan-12,99.4,115.5
22-Jan-12,98.4,115
28-Jan-12,97.1,114
4-Feb-12,95.9,112
11-Feb-12,95.3,113
18-Feb-12,93.9,111.5
25-Feb-12,93.5,111.5
3-Mar-12,92.6,110
10-Mar-12,91.4,108
17-Mar-12,90.2,106.8
24-Mar-12,90,107.5
31-Mar-12,89.9,106
5-Apr-12,90.4,106.5
12-Apr-12,89.8,106") 

track <- read.csv(con, header=TRUE)
track$Date <- as.Date( dmy(track$Date) )

track <- melt(track, 
            id.vars = c("Date"))

con <- textConnection("City,From,To
Auckland,5-Apr-12,10-Apr-12
Brussels,1-Apr-12,3-Apr-12
Cleveland,24-Jan-12,26-Jan-12
Darjeeling,18-Jan-12,19-Jan-12
Erehwon,8-Feb-12,10-Feb-12
Florence,5-Mar-12,7-Mar-12
Gandalf,25-Mar-12,27-Mar-12")

trips <- read.csv(con, header=TRUE)
trips$From <- as.Date( dmy(trips$From) )
trips$To <- as.Date( dmy(trips$To) )

# draw the Time Series data
p <- ggplot(track, 
            aes(x = Date, 
                y = value, 
                colour=variable,
                group = variable))
p <- p + geom_smooth()
p <- p + geom_point(size=3)
p <- p + geom_vline(data=trips, 
                    aes(xintercept=From),
                    colour='steelblue') # departure dates
p <- p + geom_vline(data=trips, 
                    aes(xintercept=To),
                    colour='grey80') # return dates
yrng <- c(80,120)
p <- p + scale_y_continuous(limits=yrng)

p # this works up to here, using vlines (ugly, but makes the point)

# ----------- THIS geom_rect() STOPS THIS PLOT BEING RENDERED
p2 <- p + geom_rect(aes(NULL, NULL, xmin=From, xmax=To),
                   ymin=yrng[1], ymax=yrng[2],
                   data=trips)

p2 # this gives an error message
# Error in eval(expr, envir, enclos) : object 'variable' not found

# ----------- THIS NOW WORKS, THANKS TO @smu, SEE BELOW
p2 <- p + geom_rect(aes(NULL, NULL, xmin=From, xmax=To, 
                   colour=NULL, group=NULL),
                   ymin=yrng[1], ymax=yrng[2],
               fill='orange',
               alpha=0.4,
               data=trips)
p2 # and I have added fill and alpha to prettify it a bit

As suggested by @tyler-rinker, I have edited the code to include the original error and the solution pointed out by @smu. Thanks.

  • 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-06-01T19:44:05+00:00Added an answer on June 1, 2026 at 7:44 pm

    In the first ggplot call you map the colour to ‘variable’. This mapping is still present in the geom_rect call, but no variable named ‘variable’ exists in ‘trips’. I would suggest to unmap colour using:

    p2 <- p + geom_rect(aes(NULL, NULL, xmin=From, xmax=To, colour=NULL),
                   ymin=yrng[1], ymax=yrng[2],
                   data=trips)
    

    (Haven’t tried it, but I guess it should work).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to replicate the example here; http://onertipaday.blogspot.com/2011/07/word-cloud-in-r.html Need help figuring out how to increase
i am trying to replicate DEMO 3 in this page: http://www.mudaimemo.com/p/simpledialog/ it works great
I am trying to replicate a page I have been given, and I have
I'm trying to replicate a formula from an Excel worksheet onto an ASP page,
Im trying to replicate the functionality of this page (http://www.kissfm.ro/fresh-top-40/) for a friend who
I am trying to replicate the example given by Alex Kuhl on his excellent
I am trying to replicate an example found on MSDN. I am using ASP.NET
Trying to replicate the docking controls in Visual Studio 2008. My application replays the
Im trying to replicate an effect as seen on http://www.fiat.co.uk/Showroom/#showroom/punto_evo/explore . I have made
Im trying to replicate the following sql in Nhibernate ICriteria SELECT DISTINCT AP.ID FROM

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.