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

The Archive Base Latest Questions

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

I have two plots displaying supply and demand, and one plot in which I

  • 0

I have two plots displaying supply and demand, and one plot in which I have subtracted the demand from the supply to show the resulting asymmetry. I would like to shade the area between the x-axis and the negative part of the asymmetry, to show the extent of the deficit.

I currently use the following code:

plot.asymmetry <- ggplot(data=df.overview.month, 
                         aes(x=Date.Time, y=Asymmetry)) +    
      geom_area(data=subset(df.overview.month, Asymmetry < 0),     
                         aes(x=Date.Time, y=Asymmetry)) 

However – as could be expected – this does not shade the area between geom_line and the x-axis, but only between negative values of the asymmetry data, which is something else entirely, as shown in the resulting graph:

enter image description here

Is there any way to overcome this problem?

/Edit: some example data:

time.initial <- as.POSIXct("2010-12-31 23:00:00", tz="GMT")
Date.Time<-vector()
for(i in 1:24) {
Date.Time[i] <- time.initial + i*3600
}

Demand<-vector()
for(i in 0:23) {
Demand[i+1] <- 155 + 20*sin((pi/12)*i - (pi/2)) + 10*sin((pi/4380)*i + (pi/2))
}

Supply<-vector()
for(i in 0:23) {
Supply[i+1] <- 165 + 5*sin((pi/4380)*i - (pi/2)) + rnorm(1, mean=0, sd=0.20*165)
}

df.overview.month <- data.frame(Date.Time, Demand, Supply, Asymmetry=Supply-Demand)
  • 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-01T09:31:45+00:00Added an answer on June 1, 2026 at 9:31 am

    What about this as inspiration. Now you only need to add additional data points where the asymmetry is equal to zero (like @baptiste suggested). I create a new column which is NA when the asymmetry is above zero, in this way no geom_ribbon will be drawn there. Just subsetting the data will not lead to the required plot.

    df.overview.month$Assym_ribbon = ifelse(df.overview.month$Asymmetry > 0, 
                                            NA, 
                                            df.overview.month$Asymmetry)
    ggplot(aes(x = Date.Time, y = Asymmetry), 
             data = df.overview.month) + 
       geom_line() + 
       geom_ribbon(aes(ymin = 0, ymax = Assym_ribbon), 
             data = , fill = "red")
    

    enter image description here

    Some additional notes about the way you constructed your example. The most important one is that R is vectorized. For example:

    set.seed(1)
    Supply<-vector()
    for(i in 0:23) {
      Supply[i+1] <- 165 + 
               5*sin((pi/4380)*i - 
               (pi/2)) + 
               rnorm(1, mean=0, sd=0.20*165)
    }
    

    is equivalent to:

    set.seed(1)
    i = 0:23
    Supply_vec <- 165 + 5*sin((pi/4380)*i - 
                   (pi/2)) + 
                   rnorm(length(i), mean=0, sd=0.20*165)
    
    > all.equal(Supply_vec, Supply)
    [1] TRUE
    

    In this case the reduction in code is modest, but in other (more realistic) settings using vectorization will save you dozens of lines of code.

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

Sidebar

Related Questions

I have a graph with two plots. One plot shows 10 data points and
Say that I have two figures in matplotlib, with one plot per figure: import
I have the following code which lets the user plot two points on a
I have two plots (overlaid with clear color background) which are updated every second
I have two patch plots on the same graph. One is red and the
I have a graph with two plots. The first plot, CPTScatterPlot *boundLinePlot, is very
I have two sets of data, (Ax, Ay; Bx, By). I'd like to plot
Hi I need to plot a boxplot in R. I have two matrices a
With this approach. I have a line plot graph. I want to plot 'two'
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two

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.