2016-09-14 397 views
-1

我有一个数据框(见下文),其中有几行比通过Fly列标记的每行都多(每个都是唯一的),并且每条线都通过条件栏。每条线都有四个与其在不同时间拍摄的测量值相关联。我想将X上的音量绘制为音量,将Y绘制为时间。并通过Fly下的独特线条进行颜色编码。有没有一个快速简单的方法来做到这一点?不知道如何让它工作,或者开始使用ggplot2()甚至plot()。一些帮助和指导,将不胜感激。如何使用绘图或ggplot2在R中快速绘制多条线

 Fly condition_1 volume_1 volume_2 volume_3 volume_4 
1  1 postcont  1.6  18.0  8.1  13.5 
2  2 postcont  0.0  12.0  10.1  8.5 
3  3 postcont  2.6  3.0  10.1  1.5 
4  4 postcont  13.6  13.0  10.1  15.5 

下面是代码最多绘制

data1<-read.table(file=file.choose(),header=T) 
head(data1) 
postcontexp<-subset(data1, condition_1 == "postcont") 
postcontexpVOL<- subset(# to remove unwanted columns) 
postcontexpVOL<- na.omit(postcontexpVOL) 
+2

对不起,这不是要求在基础上定制教程的好地方绘图功能。如果您[查看R标记wiki](http://stackoverflow.com/tags/r/info),有许多免费资源可供您开始使用R.您还可以在Stack Overflow上搜索其他问题。如[R在一个图上绘制多条线](http://stackoverflow.com/q/17150183/903061)。 – Gregor

+1

您的数据不整洁,这使得绘图变得困难,因为如果您没有时间变量,因为很难绘制时间,因为该信息被困在列名中。首先重塑,例如库(tidyverse); df%>%gather(time,volume,starts_with('volume'))%> mutate(time = parse_number(time))' – alistaire

+0

@ Gregor,我理解绘图的几个基本知识。我只是不想使用abline()一些次数。 – multiverse

回答

0

下面是一个使用meltreshape2dplyr以适应调整数据版本:

n <- 4 
df <- 
    data.frame(
    Ind = 1:n 
    , vol_1 = rnorm(n) 
    , vol_2 = rnorm(n) 
    , vol_3 = rnorm(n) 
    , vol_4 = rnorm(n) 
) 


melted <- 
    melt(df, id.vars = "Ind") %>% 
    mutate(Time = as.numeric(gsub("vol_","",variable))) 

ggplot(melted 
     , aes(x = Time 
      , y = value 
      , col = as.factor(Ind))) + 
    geom_line() 

enter image description here