17

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?

Chris Neff
  • 215
  • 1
  • 2
  • 6

2 Answers2

18

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.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
kohske
  • 65,572
  • 8
  • 165
  • 155
  • So the measure part is the thing I was ignoring. I suppose this is the right way, just a little annoying when you are exploring interactively. – Chris Neff Sep 27 '11 at 14:20
  • 2
    @Chris - alternatively, melt the entire data.frame as you suggested and then use `subset()` as the data argument in the `ggplot()` call if you plan on plotting different things and don't want to re `melt` – Chase Sep 27 '11 at 14:33
  • 2
    IDK what the situation in 2011 was, but in 2018 with R 3.6.1. you have to import the package `library(reshape2)` to use the `melt` function. – Mr. T Nov 20 '18 at 11:16
6

With reshape2 being deprecated, I updated @kohske answer using pivot_longer from tidyverse package.

Pivoting is explained here and involves specifying the data to reshape, second argument describes which columns need to be reshape (use - to exclude that column). Third is names_to gives the name of the variable that will be created from the data stored in the column names. Finally values_to gives the name of the variable that will be created from the data stored in the cell value, i.e. count. They also have more complex examples like numbers in column names e.g. wk1 wk2 etc.

# new suggestion
library(tidyverse)

# I subset to just the variables wanted so e.g. gear and cab are not included
mtcars.long <- mtcars %>% 
  select("mpg","disp", "hp", "wt") %>% 
  pivot_longer(-mpg, names_to = "variable", values_to = "value")

head(mtcars.long)
# # A tibble: 6 x 3
# mpg variable  value
# <dbl> <chr>     <dbl>
#   1    21 disp     160   
# 2    21 hp       110   
# 3    21 wt         2.62
# 4    21 disp     160   
# 5    21 hp       110   
# 6    21 wt         2.88


ggplot(mtcars.long, aes(mpg, value, colour = variable)) + geom_line()

Chart is:

mtcarstestchart

micstr
  • 5,080
  • 8
  • 48
  • 76