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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:43:17+00:00 2026-05-31T06:43:17+00:00

There are a few posts regarding the use of shaded areas in ggplot2, but

  • 0

There are a few posts regarding the use of shaded areas in ggplot2, but I don’t think any exactly answer my question. I have two slopes for lines across a number of conditions, and I would like to shade the region between them. Here is example data:

dat <- data.frame(cond1=c("a","a","b","b"),
              cond2=c("c","d","c","d"),
              x=c(1,5),
              y=c(1,5),
              sl=c(1,1.2,0.9,1.1),
              int=c(0,0.1,0.1,0),
              slopeU=c(1.1,1.3,1.2,1.2),
              slopeL=c(.9,1,0.7,1))

Here, sl is the mean slope parameter from a separate fitting procedure, and slopeU and slopeL represent upper and lower confidence regions on the slope estimate in each condition. The intercepts are constrained to be the same. The following code plots the best fitting lines for each condition using some faceting:

p <- ggplot(dat,aes(x=x,y=y,colour=cond1))
p <- p + facet_grid(. ~ cond2)
p <- p + geom_blank()
p <- p + geom_abline(aes(intercept=int,slope=sl,colour=cond1),data=dat)
p

I would like to add the lines defined by intercept=int, slope=slopeU and intercept=int, slope=slopeL to the plot and shade the region between them (e.g. at alpha=.5 in the corresponding cond1 colour).

I recognise that with a little manipulation I could create a data frame specifying values of these lines for at least two x values, then plot the corresponding geom_ribbon or geom_polygon to create the shaded region, however I would like to find a more elegant solution. Or is manually specifying some coordinates from the slopes and intercepts the only way? How would I best create the required data frame (which will need to have more rows than the original frame to account for all combinations of conditions and x,y pairs).

  • 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-31T06:43:19+00:00Added an answer on May 31, 2026 at 6:43 am

    Personally, I think that creating the data frames and using geom_ribbon is the elegant solution, but obviously opinions will differ on that score.

    But if you take full advantage of plyr and ggplot things can get pretty slick. Since your slopes and intercepts are all nicely stored in a dataframe anyway, we can use plyr and a custom function to do all the work:

    dat <- data.frame(cond1=c("a","a","b","b"),
              cond2=c("c","d","c","d"),
              x=c(1,5),
              y=c(1,5),
              sl=c(1,1.2,0.9,1.1),
              int=c(0,0.1,0.1,0),
              slopeU=c(1.1,1.3,1.2,1.2),
              slopeL=c(.9,1,0.7,1))
    
    genRibbon <- function(param,xrng){
        #xrng is a vector of min/max x vals in original data
        r <- abs(diff(xrng))
        #adj for plot region expansion
        x <- seq(xrng[1] - 0.05*r,xrng[2] + 0.05*r,length.out = 3)
        #create data frame
        res <- data.frame(cond1 = param$cond1,
                          cond2 = param$cond2,
                          x = x,
                          y = param$int + param$sl * x,
                          ymin = param$int + param$slopeL * x,
                          ymax = param$int + param$slopeU * x)
        #Toss the min/max x vals just to be safe; needed them 
        # only to get the corresponding y vals
        res$x[which.min(res$x)] <- -Inf
        res$x[which.max(res$x)] <- Inf
        #Return the correspondinng geom_ribbon
        geom_ribbon(data = res,aes(x = x,y=y, ymin = ymin,ymax = ymax,
                                   fill = cond1,colour = NULL),
                    alpha = 0.5)
    }
    
    ribs <- dlply(dat,.(cond1,cond2),genRibbon,xrng = c(1,5))
    

    The extra slick thing here is that I’m discarding the generated data frames completely and just returning a list of geom_ribbon objects. Then they can simply be added to our plot:

    p + ribs + 
        guides(fill = guide_legend(override.aes = list(alpha = 0.1)))
    

    I overrode the alpha aesthetic in the legend because the first time around you couldn’t see the diagonal lines in the legend.

    enter image description here

    I’ll warn you that the last line there that generates the plots also throws a lot of warnings about invalid factor levels, and I’m honestly not sure why. But the plot looks ok.

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

Sidebar

Related Questions

I know how to use locks in my app, but there still few things
Hey I know there are already a few posts about this - yet I
There are, as of now, 3,898 posts in StackOverflow regarding mouse click coordinates. They
Although I know there are hundreds of posts in stackoverflow regarding this post still,
I've red a few posts regarding image comparison, and I tried to save image
I have read a few others' questions regarding merge VS rebase, what to use
in my page there are few hidden field which are filled with the correct
It looks like there a few working solutions for using custom true type fonts
I have a webpage where there are few text fields to be filled up
I'm converting an old VB form to .NET, and there a few Buttons which

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.