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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:35:49+00:00 2026-06-14T14:35:49+00:00

I wish to plot a shaded region around a line using ggplot. The two

  • 0

I wish to plot a shaded region around a line using ggplot. The two data-sets are not directly related and I am doing some comparison between the two. Basically, I wish to plot the output which results from code below, along with shaded region having minimum and maximum y-axis values as entered in the variables ‘lower_region_values’ and ‘upper_region_values’.

Please help me with this issue.

Reproducible code starts here

library("ggplot2")
plotName <- 'Comparison of latitudinal averages'

xlims <- c(65,80) # x axis extent

input_df <- structure(list(flux = c(0.08733913, 0.1015934, 
      0.1244135, 0.1390303,0.08417182,   0.02371609), 
      model = structure(c(1L, 1L, 1L,1L, 1L, 1L),.Label = 
      c("CASA_GFED_Optimized"),  class = "factor"), lat = 
      c(79, 77, 75, 73, 71, 69)), .Names = c("flux","model",  
      "lat"),row.names = c(NA, -6L), class = "data.frame")

 lower_region_values<-c(-0.002157493,-0.004465291,-0.376925852,
     -0.312737571,-0.327533801,  -0.299551351)

 upper_region_values<-c(1.943331e-06,1.758454e-04,3.183347e-01,
      2.442368e-01,1.206353e-  01,1.572531e-02)

 sd_input_lower$model <- factor(sd_input_lower$model, 
      levels=levels(input_df$model))
 sd_input_upper$model <- factor(sd_input_upper$model, 
      levels=levels(input_df$model))

NEED a shaded region using the values in two vectors: lower_region_values and upper_region_values

 chart <- ggplot(input_df, aes(x=lat, group=model, 
      colour=model, fill=model)) +

# geom_ribbon(data = sd_input_upper, aes(ymin = -sd, ymax=sd), alpha=0.5) +

 geom_line(aes(y=flux), size=1.0) + 
      opts(title=plotName,legend.position='bottom') +
      scale_x_continuous('Latitude',limits=xlims) +   
      scale_y_continuous(expression(paste('Latitudinal average of NEE ', 
      (g~C~m^{-2}/day)))) +
      scale_colour_discrete(name='Model') +
      scale_fill_discrete(name='Model')

 print(chart) 
  • 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-14T14:35:51+00:00Added an answer on June 14, 2026 at 2:35 pm

    I would add the regions for shading into the same data.frame, because it is easier to do ggplots if they are all in the same object. My resulting plot looks a little funny to me so I think we need to make certain that when we add these regions in, they are matching the x-axis values properly in their rows.

    input_df$lower_region_values<-c(-0.002157493,-0.004465291,-0.376925852,
         -0.312737571,-0.327533801,  -0.299551351)
    
    input_df$upper_region_values<-c(1.943331e-06,1.758454e-04,3.183347e-01,
          2.442368e-01,1.206353e-01,1.572531e-02)
    

    If I understand your goals correctly, I would basically do what you were doing, but move the aes() around a little:

    chart<-
          ggplot(input_df, aes(x=lat, y=flux,group=model)) +
          geom_line(size=1.0,colour="red") + 
          opts(title=plotName,legend.position='bottom') +
          scale_x_continuous('Latitude',limits=xlims) +   
          scale_y_continuous(expression(paste('Latitudinal average of NEE ', 
          (g~C~m^{-2}/day)))) +
          geom_ribbon(aes(ymin = lower_region_values, 
          ymax=upper_region_values), colour="blue", fill="blue",alpha=0.5)
    
     print(chart) 
    

    You seem to be using an older version of ggplot2, so if you’re still learning ggplot, I would update and learn the new one, which has some significant changes.

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

Sidebar

Related Questions

I am using plotyy to plot two vectors on different y-axes. I wish to
Context: I have some data, and I wish to: plot a histogram of them
I am trying to make a contour plot of the following data using matplotlib
Good day, I wish to produce a graphic using ggplot2, but not using its
I wish to know all the pros and cons about using these two methods.
I wish to plot graphically a function using MIT scheme. In the manual of
For example. Assume I do: dev.new(width=5, height=4) plot(1:20) And now I wish to do
I wish to do this using only XAML - How to add an additional
I wish fetch data in a XML file, where Tag is a variable.. My
I wish to plot implicit functions in MATLAB. Like x^3 + xy + y^2

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.