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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:58:18+00:00 2026-06-18T09:58:18+00:00

I want to incrementally build a plot that contains several data series of different

  • 0

I want to incrementally build a plot that contains several data series of different lengths. My goal is to be able to control the appearance of each data series,
give them custom names
and to have appropriate legends. My data series are of different lengths, so
I cannot put them in a single dataframe.
In the code below I expect 4 lines: the shortest will be red, the next ones will be blue, green and black respectively

    library(ggplot2)
    set.seed(12345)
    plt <- ggplot()
    colors <- c('red', 'blue', 'green', 'black')
    for(i in seq(length(colors))) { 
      x <- seq(1, 2*i)
      y <- x * i + rnorm(length(x))
      df <- data.frame(x=x, y=y)
      plt <- plt +  geom_point(aes(x, y), data=df, color=colors[i]) + 
        geom_line(aes(x, y), data=df, color=colors[i]) 
    }
    print(plt)

This is what I get.
my plot

How can I give names to the lines and display a legend?
Is there a better way to acheive my goal?

  • 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-18T09:58:20+00:00Added an answer on June 18, 2026 at 9:58 am

    The way to do this is to create a single data frame in long format:

    Like this:

    library(ggplot2)
    set.seed(12345)
    colors <- c('red', 'blue', 'green', 'black')
    dat <- lapply(seq_along(colors), function(i){
      x <- seq(1, 2*i)
      data.frame(
        series = colors[i],
        x = x,
        y = x * i + rnorm(length(x))
      )}
    )
    dat <- do.call(rbind, dat)
    

    Now plot

    ggplot(dat, aes(x, y, color=series)) + geom_line() 
    

    enter image description here

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

Sidebar

Related Questions

Goal: Build an Android app that discovers the names and addresses of BT devices
I want to select elements that have similar incrementally-based names. Is there shorter notation
I have a series of characters that I want to match with a regular
I have 8 pages labeled Step0-Step7 that are used to save data incrementally to
I want my language backend to build functions and types incrementally but don't pollute
Want to be able to provide a search interface for a collection of objects
I'm working with continuously updated ~20GB Interbase backups that i want to replicate over
Say I have some data like this: number_stream = [0,0,0,7,8,0,0,2,5,6,10,11,10,13,5,0,1,0,...] I want to process
I need the list of files that were compiled during this run. I want
I want to build a JNI DLL using Eclipse and Cygwin. I know there

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.