corahdactyl
corahdactyl

Reputation: 1

How to plot one column vs the rest in R

I have a data set where the [,1] is time and then the next 14 are magnitudes. I would like to scatter plot all the magnitudes vs time on one graph, where each different column is gridded (layered on top of one another)

I want to use the raw data to make these graphs and came make them separately but would like to only have to do this process once.

data set called A, the only independent variable is time (the first column)

df<-data.frame(time=A[,1],V11=A[,2],V08=A[,3],
 V21=A[,4],V04=A[,5],V22=A[,6],V23=A[,7],
 V24=A[,8],V25=A[,9],V07=A[,10],xxx=A[,11],
 V26=A[,12],PV2=A[,13],V27=A[,14],V28=A[,15],
 NV1=A[,16])

I tried the code mentioned by @VlooO but it scrunched the graphs making them too hard to decipher and each had its own axes. All my graphs can be on the same axes just separated by their headings.

When looking at the ggplots I Think that would be a perfect program for what I want.

ggplot(data=df.melt,aes(x=time,y=???))

I confused what my y should be since I want to reference each different column.

Thanks R community

Upvotes: 0

Views: 7375

Answers (4)

corahdactyl
corahdactyl

Reputation: 1

SOLUTION: After looking through a bunch more problems and playing around a bit more with ggplot2 I found a code that works pretty great. After I made my data frame (stated above), here is what i did

> df.m<- melt(df,"time")
ggplot(df.m, aes(time, value, colour = variable)) + geom_line() +
+ facet_wrap(~ variable, ncol = 2)

I would post the image but I don't have enough reputation points yet.

I still don't really understand why "value" is placed into the y position in aes(time, value,...) If anyone could provided an explanation that would be greatly appreciated. My last question is if anyones knows how to make the subgraphs titles smaller.

Can I use cex.lab=, cex.main= in ggplot2?

Upvotes: 0

Greg Snow
Greg Snow

Reputation: 49650

If you don't need ggplot2, then the matplot function for base graphics can be used to do what you want in one command.

Upvotes: 1

agstudy
agstudy

Reputation: 121588

here some hints since you don't provide a reproducible example , neither you show what you have tried :

  1. Use list.files to go through all your documents
  2. Use lapply to loop over the result of the previous step and read your data
  3. Put your data in the long format using melt from reshape2 and the variable time as id.
  4. Use ggplot2 to plot using the variable as aes color/group.

    library(ggplot2)
    library(reshape2)
    invisible(lapply(list.files(pattern=...),{
          dt = read.table(x)
          dt.l = melt(dt,id.vars='time')
          print(ggplot(dt.l)+geom_line(aes(x=time,y=value,color=variable))
        }))
    

Upvotes: 3

Rentrop
Rentrop

Reputation: 21507

Hope i understand you correctly:

df<-data.frame(time=rnorm(10),A=rnorm(10),B=rnorm(10),C=rnorm(10))

par(mfrow=c(length(df)-1,1))
sapply(2:length(df), function(x){
  plot(df[,c(1,x)])
})

The result would be

Rplot

Upvotes: 3

Related Questions