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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:52:19+00:00 2026-05-23T04:52:19+00:00

I need to plot a time series with ggplot2. For each point of the

  • 0

I need to plot a time series with ggplot2. For each point of the time series I also have some quantiles, say 0.05, 0.25, 0.75, 0.95, i.e. I have five data for each point. For example:

time           quantile=0.05  quantile=0.25 quantile=0.5  quantile=0.75   quantile=0.95
00:01          623.0725       630.4353      903.8870       959.1407       1327.721
00:02          623.0944       631.3707      911.9967      1337.4564       1518.539
00:03          623.0725       630.4353      903.8870      1170.8316       1431.893
00:04          623.0725       630.4353      903.8870      1336.3212       1431.893
00:05          623.0835       631.3557      905.4220      1079.6623       1452.260
00:06          623.0835       631.3557      905.4220      1079.6623       1452.260
00:07          623.0835       631.3557      905.4220      1079.6623       1452.260
00:08          623.0780       631.3483      905.3496      1056.3719       1375.610
00:09          623.0671       630.4275      903.8839      1170.8196       1356.963
00:10          623.0507       630.0261      741.8475      1006.1208       1462.271

Ideally, I would like to have the 0.5 quantile as a black line and the others as shaded color intervals surrounding the black line. What’s the best way to do this? I’ve been looking around with no luck, I can’t find examples of this, even less with ggplot2.

Any help would be appreciated.

Salud!

  • 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-23T04:52:20+00:00Added an answer on May 23, 2026 at 4:52 am

    Does this do what you want? The trick to ggplot is understanding that it expects data in long format. This often means that we have to transform the data before it is ready to plot, usually with melt().

    After reading your data in with textConnection() and creating an object named dat, here are the steps you’d take:

    #Melt into long format 
    dat.m <- melt(dat, id.vars = "time")
    
    #Not necessary, but if you want different line types depending on quantile, here's how I'd do it
    dat.m <- within(dat.m
      , lty <- ifelse(variable == "quantile.0.5", 1
        , ifelse(variable %in% c("quantile.0.25", "quantile.0.75"),2,3)
        )
    )
    
    #plot it
    ggplot(dat.m, aes(time, value, group = variable, colour = variable, linetype = lty)) + 
      geom_line() +
      scale_colour_manual(name = "", values = c("red", "blue", "black", "blue", "red"))
    

    Gives you:

    enter image description here

    After reading your question again, maybe you want shaded ribbons outside the median estimate instead of lines? If so, give this a whirl. The only real trick here is that we pass group = 1 as an aesthetic so that geom_line() will behave properly with factor / character data. Previously, we grouped by the variable which served the same effect. Also note that we are no longer using the melted data.frame, as the wide data.frame will suit us just fine in this case.

    ggplot(dat, aes(x = time, group = 1)) +
      geom_ribbon(aes(ymin = quantile.0.05, ymax = quantile.0.95, fill = "05%-95%"), alpha = .25) + 
      geom_ribbon(aes(ymin = quantile.0.25, ymax = quantile.0.75, fill = "25%-75%"), alpha = .25) +
      geom_line(aes(y = quantile.0.5)) +
      scale_fill_manual(name = "", values = c("25%-75%" = "red", "05%-95%" = "blue")) 
    

    enter image description here

    Edit: To force a legend for the predicted value

    We can use the same approach we used for the geom_ribbon() layers. We’ll add an aesthetic to geom_line() and then set the values of that aesthetic with scale_colour_manual():

    ggplot(dat, aes(x = time, group = 1)) +
      geom_ribbon(aes(ymin = quantile.0.05, ymax = quantile.0.95, fill = "05%-95%"), alpha = .25) + 
      geom_ribbon(aes(ymin = quantile.0.25, ymax = quantile.0.75, fill = "25%-75%"), alpha = .25) +
      geom_line(aes(y = quantile.0.5, colour = "Predicted")) +
      scale_fill_manual(name = "", values = c("25%-75%" = "red", "05%-95%" = "blue")) +
      scale_colour_manual(name = "", values = c("Predicted" = "black"))
    

    There may be more efficient ways to do that, but that’s the way I’ve always used and have had pretty good success with it. YMMV.

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

Sidebar

Related Questions

I need to read some data from an input file and plot a graph
I have plots of 3-axis accelerometer time-series data (t,x,y,z) in separate subplots I'd like
I have data representing the position of particles for multiple time steps and need
I need to plot the transformation of a 3d object against time. I have
I need to read and plot data in real time from multiple Android phones
I need plot a curve for my data , the source is like :
need ask you about some help. I have web app running in Net 2.0.
Need some help about with Memcache. I have created a class and want to
I have a data frame with a series of times in the following format:
I'm trying to plot two sets of data wrt to time on the same

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.