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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:42:33+00:00 2026-06-10T13:42:33+00:00

I have created a stacked area plot with ggplot2 and added vertical lines at

  • 0

I have created a stacked area plot with ggplot2 and added vertical lines at some positions on the x-axis.

I now want to name the sections that are separated by these vertical lines. An example of it could look like it is shown on the example plot. Other solutions are also welcome.
I have a vector of breaks (x-axis) and a vector of the names for the intervals.

Code:

library(ggplot2)
d <- read.delim(...)
x_breaks = c(-3999,1,599,4076,7557,11556)

png(output, width=800, height=400)

ggplot(d, aes(x=p, y=c, group=Groups, fill=Groups)) +
geom_area(position="stack") +
opts(title="testtestest",
...) +
scale_x_continuous(expand=c(0,0), breaks=x_breaks) +
scale_y_continuous(expand=c(0,0)) +
geom_vline(xintercept=x_breaks[which(x_breaks != min(x_breaks) & x_breaks != max(x_breaks))])

dev.off()

How to name the sections seperated by the vertical lines?

  • 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-10T13:42:34+00:00Added an answer on June 10, 2026 at 1:42 pm

    Without data, it’s a bit hard to reproduce your example (your breaks, etc). But the following should get you started.

    One fairly straightforward solution is to use ggplot2’s annotate() function to add text annotations to the plot panel midway between the vertical lines.

    # Load packages
    library (ggplot2)
    library(grid)
    
    # Some data
    df = data.frame(x = 1:10, y = 1:10)
    
    # Base plot
    p <- ggplot(df, aes(x,y)) + geom_point() + 
      scale_x_continuous(limits = c(0, 11), expand = c(0,0)) +
      geom_vline(xintercept = 3) + geom_vline(xintercept = 7)
    
    # Add the annotations
    p + annotate("text", x = .5*(0+3), y = 11, label = "Part 1") +
         annotate("text", x = .5*(3+7), y = 11, label = "Part 2") +
         annotate("text", x = .5*(7+11), y = 11, label = "Part 3")
    

    The result is:

    enter image description here

    Or a solution that is close to your example plot as far as the annotations are concerned. It uses annotation_custom() to draw lines and to position text outside the plot panel. Even though the annotations are drawn outside the plot panel, the positioning of the annotations is in terms of the data coordinates. The bottom margin in the base plot is widened to give room for the annotations. The solution requires code to override ggplot’s clipping of plot elements to the plot panel.

    Update opts is deprecated; using theme instead.

    # Base plot
    p <- ggplot(df, aes(x,y)) + geom_point() + 
      scale_x_continuous(limits = c(0, 11), expand = c(0,0)) +
      geom_vline(xintercept = 3) + geom_vline(xintercept = 7) +
      theme(plot.margin = unit(c(1,1,4,1), "lines"))
    
    # Create the text Grobs
    Text1 = textGrob("Part 1")
    Text2 = textGrob("Part 2")
    Text3 = textGrob("Part 3")
    
    # Add the annotations
    # Segment 1
    p1 = p + 
        annotation_custom(grob = linesGrob(), xmin = 0, xmax = 0, ymin = -1, ymax = -.75) +
        annotation_custom(grob = linesGrob(), xmin = 0, xmax = 3, ymin = -1, ymax = -1) +
        annotation_custom(grob = linesGrob(), xmin = 3, xmax = 3, ymin = -1, ymax = -.75) +
        annotation_custom(grob = Text1,  xmin = 0, xmax = 3, ymin = -1.25, ymax = -1.25)
    
    # Segment 2
    p1 = p1 + 
        annotation_custom(grob = linesGrob(), xmin = 3, xmax = 7, ymin = -1, ymax = -1) +
        annotation_custom(grob = linesGrob(), xmin = 7, xmax = 7, ymin = -1, ymax = -.75) +
        annotation_custom(grob = Text2,   xmin = 3, xmax = 7, ymin = -1.25, ymax = -1.25) 
    
    # Segment 3
    p1 = p1 + 
        annotation_custom(grob = linesGrob(), xmin = 7, xmax = 11, ymin = -1, ymax = -1) +
        annotation_custom(grob = linesGrob(), xmin = 11, xmax = 11, ymin = -1, ymax = -.75) +
        annotation_custom(grob = Text3,  xmin = 7, xmax = 11, ymin = -1.25, ymax = -1.25)
    
    # Code to override clipping
    gt <- ggplot_gtable(ggplot_build(p1))
    gt$layout$clip[gt$layout$name=="panel"] <- "off"
    grid.draw(gt)
    

    The result is:

    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 created some JQuery that will expand a div 'popup' on hover and
Have created a c++ implementation of the Hough transform for detecting lines in images.
I have created a SSRS 2008 report.I have created a matrix.I want to make
I have created a stacked canvas in Oracle Forms 10g.Also I have 2 buttons
I created a stacked bar chart in iReport. Some of the labels under each
I have created a stacked bar chart in which I show a count on
I have created a QWidget with a bunch of QToolButtons in it and I
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created a JSP / servlets application running in Tomcat 7. It runs
I have created an EDMX in visual studio 2010 SP1. It has been built

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.