简体   繁体   中英

Plot Multiple columns against one column in R

First time here!

I have a dataset (60k obs. of 86 variables) variable 86 is a time variable and the rest are just random numbers. I'm trying to plot each column, the first 85, against the time variable. The time variable could be detached from the data frame if needed.

I'm looking to plot each with a line graph since is a time series data set and each should be on its own plot. Can this be done in ggplot2?

Thanks Justin

Create some example data. Here I'm just using 3 columns (x, y, z) of 100 observations, plus a time column.

dat = data.frame(x=rnorm(100), y=rnorm(100), z=rnorm(100), time=1:100)

Use reshape2::melt to convert your data into the format that ggplot likes:

library('reshape2')
dat_melted = dat_melted = melt(dat, id.vars='time')

Then you can plot them in facets:

library('ggplot2')
ggplot(dat_melted, aes(time, value)) + 
  geom_line() + 
  facet_wrap(~variable)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM