简体   繁体   English

具有重塑(熔化函数)的R ggplot2选择性地绘制数据集

[英]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来绘制一些数据集,但是我想绘制它们以便每个数据集都有自己的geom_line函数,这样我就可以将这些行分开并在需要时隐藏它们。

 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. 表A应该是一个geom_line (图中的线)表B,第二个geom_line ,依此类推。 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. 我给你的数据每行只有两个点,所以想象你从每个表(A,B,C和D)得到数百个点,因此这个图上会有四行。 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)... 所以我想的是做一个单独的+ 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: 编辑2:这就是图形现在的样子,它应该看起来像这样,但是有4个geom_line调用而不是现在的调用:

在此输入图像描述

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 OP澄清后编辑

With ggplot2( Lattice also) you can combine data sources and subsetting for each layer 使用ggplot2(也可以使用Lattice),您可以为每个图层组合数据源和子集

Here for example I choose to show only 2 lines 例如,我选择仅显示2行

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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