简体   繁体   中英

R ggplot2 with reshape (melt function) selectively graph data sets

I am using ggplot to graph a number of data sets, however I would like to plot them so that each data set has its own geom_line function so that I can seperate the lines out and hide them if required.

 ggplot(MeanFrameMelt, aes(x=variable, y=value, 
           color=Legend, group=Legend)) + geom_line()

Input table after transformed with the melt function in the package reshape:

Legend        variable  value
table_A.txt V1  0.008927491
table_B.txt V1  0.009080929
table_C.txt V1  0.008513332
table_D.txt V1  0.008337751
table_A.txt V2  0.008957742
table_B.txt V2  0.009100265
table_C.txt V2  0.008508966

table A should be one geom_line (line on the graph) table B a second geom_line and so on. Is this possible or do I have to go back and change the melting of the previous data frame?

Edit: ok this is the melt function:

library(plyr)
library(reshape)    
MeanFrameMelt <- melt(MeanFrame2, id.vars="Legend")

The data i've given you is of only two points for each line, so imagine you have hundreds of points from each table (A, B, C, and D) hence there will be four lines on this graph. I want to be able to switch off each line with a checkbox, but for this I need to have a unique identifier for each line which will allow me to do this. So what I was thinking is to do a seperate + geom_line(for table A) + geom_line(for table B) + geom_line(for table C)...

I hope this clarifies thinks a bit.

Edit2: this is what the graph looks like now, and it should look like this after aswell, but with 4 geom_line calls instead of just one that it has now:

在此输入图像描述

I think this is close to what you want:

ggplot(MeanFrameMelt, aes(x=variable, y=value, 
       color=Legend, group=Legend))+ geom_line(aes(linetype=Legend))

Edit After OP clarification

With ggplot2( Lattice also) you can combine data sources and subsetting for each layer

Here for example I choose to show only 2 lines

library(ggplot2)
  ggplot(dat, aes(x=variable, y=value, ,
                          color=Legend, group=Legend))+ 
  geom_line(subset= .(Legend %in% c('table_A.txt','table_D.txt')))

在此输入图像描述

You can bind your check box to the list of line to show.

 geom_line(subset= .(Legend %in% visibleCheckedList))

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