r - Plotting each column of a dataframe as one line using ggplot -


the whole dataset describes module (or cluster if prefer).

in order reproduce example, dataset available at: https://www.dropbox.com/s/y1905suwnlib510/example_dataset.txt?dl=0

(54kb file)

you can read as:

test_example <- read.table(file='example_dataset.txt') 

what have in plot this

on plot, x-axis timepoints column, , y-axis columns on dataset, except last 3 columns. used facet_wrap() group conditionid column.

this want, way achieved following code:

plot <- ggplot(dataset, aes(x=timepoints)) plot <- plot + geom_line(aes(y=dataset[,1],colour = dataset$inmodule)) plot <- plot + geom_line(aes(y=dataset[,2],colour = dataset$inmodule)) plot <- plot + geom_line(aes(y=dataset[,3],colour = dataset$inmodule)) plot <- plot + geom_line(aes(y=dataset[,4],colour = dataset$inmodule)) plot <- plot + geom_line(aes(y=dataset[,5],colour = dataset$inmodule)) plot <- plot + geom_line(aes(y=dataset[,6],colour = dataset$inmodule)) plot <- plot + geom_line(aes(y=dataset[,7],colour = dataset$inmodule)) plot <- plot + geom_line(aes(y=dataset[,8],colour = dataset$inmodule)) ... 

as can see not automated. thought putting in loop, like

columns <- dim(dataset)[2] - 3 (i in seq(1:columns)) {   plot <- plot + geom_line(aes(y=dataset[,i],colour = dataset$inmodule)) } (plot <- plot + facet_wrap(  ~ conditionid, ncol=6) ) 

that doesn't work. found topic use loop plot multiple lines in single plot ggplot2 corresponds problem. tried solution given melt() function.

the problem when use melt on dataset, lose information of timepoints column plot x-axis. how did:

data_melted <- dataset as.character(data_melted$timepoints) dataset_melted <- melt(data_melted) 

i tried using aggregate

aggdata <-aggregate(dataset, by=list(dataset$conditionid), fun=length) 

now aggdata @ least have information on how many timepoints each conditionid have, don't know how proceed here , combine on ggplot.

can suggest me approach. know use ugly solution of creating new datasets on loop rbind(also given in link), don't wanna that, sounds inefficient. want learn right way.

thanks

you have specify id.vars in call melt.data.frame keep information need. in call ggplot need specify correct grouping variable same result before. here's possible solution:

melted <- melt(dataset, id.vars=c("timepoints", "inmodule", "conditionid")) p <- ggplot(melted, aes(timepoints, value, color = inmodule)) +   geom_line(aes(group=paste0(variable, inmodule))) p 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -