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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:55:26+00:00 2026-05-23T10:55:26+00:00

Suppose I have the following data frames: df1 = data.frame(c11 = c(1:5), c12 =

  • 0

Suppose I have the following data frames:

df1 = data.frame(c11 = c(1:5), c12 = c(1:5))
df2 = data.frame(c21 = c(1:5), c22 = (c(1:5))^0.5)
df3 = data.frame(c31 = c(1:5), c32 = (c(1:5))^2)

I want to plot these as lines in the same plot/panel. I can do this by

p <- ggplot() + geom_line(data=df1, aes(x=c11, y = c12)) + 
     geom_line(data=df2, aes(x=c21,y=c22)) + 
     geom_line(data=df3, aes(x=c31, c32))

All these will be black. If I want them in a different color, I can specify the color explicitly as an argument to geom_line(). My question is can I specify a list of a few colors, say 5 colors, such as, red, blue, green, orange, gray, and use that list so that I do not have to explicitly specify the colors as an argument to geom_line() in case of each line. If the plot p contains 2 geom_line() statements then it will color them red and blue respectively. If it contains 3 geom_line statements, it will color them red, blue and green. Finally, how can I specify the legend for these plots. Even if I can give the colors as a vector at the end of p that would be great. Please let me know if the question is not clear.

Thanks.

  • 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-23T10:55:26+00:00Added an answer on May 23, 2026 at 10:55 am

    ggplot2 works best if you work with a melted data.frame that contains a different column to specify the different aesthetics. Melting is easier with common column names, so I’d start there. Here are the steps I’d take:

    • rename the columns
    • melt the data which adds a new variables that we’ll map to the colour aesthetic
    • define your colour vector
    • Specify the appropriate scale with scale_colour_manual

    ‘

    names(df1) <- c("x", "y")
    names(df2) <- c("x", "y")
    names(df3) <- c("x", "y")
    
    newData <- melt(list(df1 = df1, df2 = df2, df3 = df3), id.vars = "x")
    
    #Specify your colour vector
    cols <- c("red", "blue", "green", "orange", "gray")
    
    #Plot data and specify the manual scale
    ggplot(newData, aes(x, value, colour = L1)) + 
      geom_line() +
      scale_colour_manual(values = cols)
    

    Edited for clarity

    The structure of newData:

    'data.frame':   15 obs. of  4 variables:
     $ x       : int  1 2 3 4 5 1 2 3 4 5 ...
     $ variable: Factor w/ 1 level "y": 1 1 1 1 1 1 1 1 1 1 ...
     $ value   : num  1 2 3 4 5 ...
     $ L1      : chr  "df1" "df1" "df1" "df1" ...
    

    And the plot itself:

    enter image description here

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

Sidebar

Related Questions

Suppose you have a data frame with the following structure: df <- data.frame(a=c(1,2,3,4), b=c(job1;job2,
Suppose I have the following data.frame : foo <- data.frame(ocean=c('Atlantic', 'Pacific'), city=c('Boston', 'San Francisco'),
Suppose we have the following table data: ID parent stage submitted 1 1 1
Suppose I have the following types of data: class Customer { String id; //
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
Consider the following data model: Suppose I have a table called SuperAwesomeData where each
Suppose I have following data in my game. I have developed game in cocos2d.
Suppose I have a table with following data: gameId difficultyLevel numberOfQuestions -------------------------------------------- 1 1
Suppose we have a simple database containing following data: name apple pear banana grape
Suppose i have the following raw data in excel spreadsheet: [Name ] [ID ]

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.