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

  • Home
  • SEARCH
  • 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 7027783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:17:39+00:00 2026-05-28T00:17:39+00:00

How do I configure a ggplot2 step plot so that when the value being

  • 0

How do I configure a ggplot2 step plot so that when the value being plotted is over a certain level it is one colour and when it is below that certain level it is another colour? (Ultimately I would like to specify the colours used.)

My first thought was that this would be a simple issue that only required me to add a column to my existing data frame and map this column to the aes() for geom_step(). That works to a point: I get two colours, but they overlap as shown in this image:
geom_step() plot with overlapping colours

I have searched SO for the past several hours and found many similar but not identical questions. However, despite trying a wide variety of combinations in different layers I have not been able to resolve the problem. Code follows. Any help much appreciated.

require(ggplot2)

tmp <- structure(list(date = structure(c(1325635200, 1325635800, 1325636400, 
1325637000, 1325637600, 1325638200, 1325638800, 1325639400, 1325640000, 
1325640600, 1325641200, 1325641800, 1325642400, 1325643000, 1325643600, 
1325644200, 1325647800, 1325648400, 1325649000, 1325649600, 1325650200, 
1325650800, 1325651400, 1325652000, 1325652600, 1325653200, 1325653800, 
1325654400, 1325655000, 1325655600, 1325656200, 1325656800), tzone = "", tclass = c("POSIXct", 
"POSIXt"), class = c("POSIXct", "POSIXt")), Close = c(739.07, 
739.86, 740.41, 741.21, 740.99, 741.69, 742.64, 741.34, 741.28, 
741.69, 741.6, 741.32, 741.95, 741.86, 741.02, 741.08, 742.08, 
742.88, 743.19, 743.18, 743.78, 743.65, 743.66, 742.78, 743.34, 
742.81, 743.31, 743.81, 742.91, 743.09, 742.47, 742.99)), .Names = c("date", 
"Close"), row.names = c(NA, -32L), class = "data.frame")
prevclose <- 743
tmp$status <- as.factor(ifelse (tmp$Close> prevclose, "Above", "Below"))

ggplot() +
    geom_step(data = tmp,aes(date, Close, colour = status))
  • 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-28T00:17:39+00:00Added an answer on May 28, 2026 at 12:17 am

    You need group = 1 in aes:

    # top panel
    ggplot(tmp, aes(date, Close, colour = status, group = 1)) + 
      geom_step() + scale_colour_manual(values = c("pink", "green"))
    

    Maybe you want to do something like this:

    # make sure that data is sorted by date
    tmp2 <- arrange(tmp, date)
    
    # add intermittent column between below/above
    tmp3 <- tmp2[1, ]
    for (i in seq(nrow(tmp2))[-1]) {
        if (tmp2[i-1, ]$status != tmp2[i, ]$status) {
            tmp3 <- rbind(tmp3,
                          transform(tmp2[i, ], Close = prevclose, status = tmp2[i-1, ]$status),
                          transform(tmp2[i, ], Close = prevclose))
        }
        tmp3 <- rbind(tmp3, tmp2[i, ])
    }
    
    # bottom panel
    ggplot(tmp3, aes(date, Close, colour = status, group = 1)) + geom_step() +
      scale_colour_manual(values = c("pink", "green"))
    

    enter image description here

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

Sidebar

Related Questions

How would one configure trac to allow anonymous submission of tickets?
I'm trying to configure a dedicated server that runs ASP.NET to send mail through
How can I configure hibernate sessionFactory in Spring such that it uses the hibernate.cfg.xml
How do I configure ASP.NET (or ASP.NET MVC more specifically) to treat certain folders
I configure the recovery for Windows services to restart with a one minute delay
How to configure emacs 23.1.1 on osx so that file drag and drop on
Is it possible to configure MySQL so that it has some kind of execution
I'd like to configure the Windsor container so that a single, singleton-style instance can
How do I configure Apache to forward a URLa of a certain extension, say
The configure file test.conf is as below: <ocsigen> <server> <port>*:8000</port> <logdir>/home/zaxis/tmp/log/</logdir> <datadir>/home/zaxis/tmp/data</datadir> <user>zaxis</user> <group>wheel</group>

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.