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

  • Home
  • SEARCH
  • 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 6619487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:58:31+00:00 2026-05-25T20:58:31+00:00

I often run into an issue where I have a data frame that has

  • 0

I often run into an issue where I have a data frame that has a single x variable, one or more facet variables, and multiple different other variables. Sometimes I would like to simultaneously plot different y variables as separate lines. But it is always only a subset I want. I’ve tried using melt to get “variable” as a column and use that, and it works if I want every single column that was in the original dataset. Usually I don’t.

Right now I’ve been doing things really roundabout it feels like. Suppose with mtcars I want to plot disp, hp, and wt against mpg:

ggplot(mtcars, aes(x=mpg)) + 
  geom_line(aes(y=disp, color="disp")) + 
  geom_line(aes(y=hp, color="hp")) + 
  geom_line(aes(y=wt, color="wt"))

This feels really redundant. If I first melt mtcars, then all variables will get melted, and then I will wind up plotting other variables that I don’t want to.

Does anyone have a good way of doing this?

  • 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-25T20:58:32+00:00Added an answer on May 25, 2026 at 8:58 pm

    ggplot always prefers long format dataframe, so melt it:

    library(reshape2)
    mtcars.long <- melt(mtcars, id = "mpg", measure = c("disp", "hp", "wt"))
    ggplot(mtcars.long, aes(mpg, value, colour = variable)) + geom_line()
    

    There are many other options for doing this transformation. You can see the R-FAQ on converting data from wide to long for an overview.

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

Sidebar

Related Questions

I often run into the problem that I have one stream full of data
I often run into code that has to perform lots of checks and ends
One of the problems I often run into, is that I will include '
i often run into a problem when variables/attributes have the wrong type. The Problem
Something that I often run into with my users is their desire to aquire
As a web developer you often run into problems that might be solved very
Often when learning about .NET I run into things that hail from the COM
I'm sure others have run into this problem too... I often watch videos in
Very often in code added by my more .NET oriented colleagues, I'll run into
I often run into a situation where I want to have a set of

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.