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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:07:47+00:00 2026-06-13T23:07:47+00:00

This code creates a nice plot but I would like to add a horizontal

  • 0

This code creates a nice plot but I would like to add a horizontal black line at y=50 AND have the legend show a black line with the text “cutoff” in the legend, but leave points in the legend for the sources. I can add the line with geom_line but cannot get the line in the legend.

    library(ggplot2)
    the.data <- read.table( header=TRUE, sep=",", 
    text="source,year,value
    S1,1976,56.98
    S1,1977,55.26
    S1,1978,68.83
    S1,1979,59.70
    S1,1980,57.58
    S1,1981,61.54
    S1,1982,48.65
    S1,1983,53.45
    S1,1984,45.95
    S1,1985,51.95
    S1,1986,51.85
    S1,1987,54.55
    S1,1988,51.61
    S1,1989,52.24
    S1,1990,49.28
    S1,1991,57.33
    S1,1992,51.28
    S1,1993,55.07
    S1,1994,50.88
    S2,1993,54.90
    S2,1994,51.20
    S2,1995,52.10
    S2,1996,51.40
    S3,2002,57.95
    S3,2003,47.95
    S3,2004,48.15
    S3,2005,37.80
    S3,2006,56.96
    S3,2007,48.91
    S3,2008,44.00
    S3,2009,45.35
    S3,2010,49.40
    S3,2011,51.19") 
    ggplot(the.data, aes( x = year, y = value ) ) + 
        geom_point(aes(colour = source)) + 
        geom_smooth(aes(group = 1))
  • 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-13T23:07:48+00:00Added an answer on June 13, 2026 at 11:07 pm

    (1) Try this:

    cutoff <- data.frame( x = c(-Inf, Inf), y = 50, cutoff = factor(50) )
    ggplot(the.data, aes( year, value ) ) + 
            geom_point(aes( colour = source )) + 
            geom_smooth(aes( group = 1 )) + 
            geom_line(aes( x, y, linetype = cutoff ), cutoff)
    

    screenshot

    (2) Regarding your comment, if you don’t want the cutoff listed as a separate legend it would be easier to just label the cutoff line right on the plot:

    ggplot(the.data, aes( year, value ) ) + 
        geom_point(aes( colour = source )) + 
        geom_smooth(aes( group = 1 )) + 
        geom_hline(yintercept = 50) + 
        annotate("text", min(the.data$year), 50, vjust = -1, label = "Cutoff")
    

    screenshot

    Update

    This seems even better and generalizes to mulitple lines as shown:

    line.data <- data.frame(yintercept = c(50, 60), Lines = c("lower", "upper"))
    ggplot(the.data, aes( year, value ) ) + 
            geom_point(aes( colour = source )) + 
            geom_smooth(aes( group = 1 )) + 
            geom_hline(aes(yintercept = yintercept, linetype = Lines), line.data)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this code... seems nice and elegant, but apparently the framework don't like
This code creates a nice calendar ( original code ), but I'm trying to
Hi I have been working on this code that creates a table with radio
I have this JavaScript code that creates a table, and puts in the rows
I have this code, which creates an image, and then adds some effects to
I have this function in a Code Igniter model that creates a new video.
I got this nice code, which I have no idea why doesn't work. It
Well ive got this code that creates a table from JSON data. If a
How can I write this code more cleanly/concisely? /// <summary> /// Creates a set
I am creating a custom code generator using c#... this generator now only creates

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.