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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:03:24+00:00 2026-05-26T10:03:24+00:00

I have a geom_area plot that looks like this: I want to color everything

  • 0

I have a geom_area plot that looks like this: area plot

I want to color everything above the x-axis green and everything below the x axis red. I have a category column in my data that contains the string “positive” for all positive values and “negative” for all negative values, so I tried simply doing fill = category and using scale_fill_manual to set positive to green and negative to red, but that gives me this:
colored area plot

Green the above the x looks right, but the red below the axis isn’t right. I’ve checked my data and there are no negative data points where it’s coloring red after Oct20, and using geom_point instead I get the correct colors.

Here’s a sample of my data:

created                 score   category
2011-10-19 21:26:19     2   positive
2011-10-19 22:50:33    -2   negative
2011-10-20 15:12:38    -2   negative
2011-10-20 17:19:24    -2   negative
2011-10-20 22:12:44     2   positive
2011-10-20 22:16:57     4   positive
2011-10-21 08:22:53     2   positive

and here’s the code I’m using to make the plot:

ggplot(data = df, aes(x = created, y = score, colour = category)) + geom_point(aes(fill = category)) + scale_fill_manual(values = c("positive" = "green", "negative" = "red"))

My problem might be related to this previous question.

  • 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-26T10:03:25+00:00Added an answer on May 26, 2026 at 10:03 am

    You need to make a new grouping variable for each positive/negative segment. To make the transitions less “blocky”, you can just first interpolate the data:

    require(ggplot2)
    
    # Load data
    df = read.table('data.txt', header=T)
    df$created = as.POSIXct(df$created, tz='UTC')
    
    # Interpolate data
    lin_interp = function(x, y, length.out=100) {
        approx(x, y, xout=seq(min(x), max(x), length.out=length.out))$y
    }
    created.interp = lin_interp(df$created, df$created)
    created.interp = as.POSIXct(created.interp, origin='1970-01-01', tz='UTC')
    score.interp   = lin_interp(df$created, df$score)
    df.interp = data.frame(created=created.interp, score=score.interp)
    
    # Make a grouping variable for each pos/neg segment
    cat.rle = rle(df.interp$score < 0)
    df.interp$group = rep.int(1:length(cat.rle$lengths), times=cat.rle$lengths)
    
    # Plot
    dev.new(width=6, height=4)
    ggplot(data = df.interp, aes(x = created, y = score, fill=score>0, group=group)) + geom_area() + scale_fill_manual(values = c('green', 'red'))
    

    enter image description here

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

Sidebar

Related Questions

I have a facetted plot like this: ggplot(mtcars, aes(x = hp, y = mpg))
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I have PostgreSQL+PostGIS. I java i want to call this query: SELECT num,id,mydata, asText(the_geom)
I have a standalone Java application below that is: Generating a random line Applied
In a ggplot2 density plot (geom_density) I have the following y-axis labels 0.000 0.005
I have some data that is constrained below a 1:1 line. I would to
I have generated the following plot using the R code that follows it: ggplot(lengths,
I have some list of dicts like this: list1 = [ dict(RL = 'rl1',
This is my main class that i want to implement the mouse button. I
have written this little class, which generates a UUID every time an object of

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.