2015-06-14 62 views
0

我是研究员,而不是程序员。R:使用曲线绘图中的多行线

我经常使用drc包来分析数据。在那里你可以定义一个曲线,它会在一个曲线中绘制多条曲线。

我需要同样的事情,只是正常的情节,但显然是我的知识很少这样做:(

我的数据是这样的:

Time  Type Material  Value1  Value2 
1 1   A   X   34  123 
1 3   A   X   44  164 
1 1   B   X   56  234 
1 2   B   X   23  145 
1 3   B   X   45  343 
1 1   A   Y   45  243 

... 

现在我想例如,第一一绘制值1〜时间,并为每个材料一个自己的行,然后可能是值2〜时间,并为每个类型自己的行

可能意图是一个脚本,你在开始声明哪一列包含x,其中y以及哪个曲线。它会将它绘制成drc包。

我尝试使用split()subset然后cbindmatplot,但我有问题,因为有时有时间值遗漏。

我也想尝试reshape2,但无法安装软件包。

是否有一些更简单的解决方案(类似于drc)包?

感谢您的帮助

+0

的GGPLOT2包可以用'ggplot做到这一点(yourdata,AES(X =时间,Y =值1,颜色=材料))+ geom_line()' 。 (使用'Value2'替换'Value1'和使用'Type'替换您的第二个plot') –

回答

0

您有重复(材质,类型)的情况下,行不会出现挪用。试试这个,用点。

tms=range(df[,"Time"]) 
ylims=range(df[,c("Value1","Value2")]) 
plot(NA,type="n",xlim=tms,ylim=ylims,xlab="Time",ylab="Val") 
df=df[with(df,order(Material,Time)),] 
mats=unique(df$Material) 
sapply(1:length(mats),function(ma){points(df[df$Material==mats[ma],"Time"], 
         df[df$Material==mats[ma],"Value1"], 
         col=ma,pch=15 ) 
}) 
df=df[with(df,order(Type,Time)),] 
library(RColorBrewer) 
coly<-brewer.pal(8,"Accent")[8:1] 
typs=unique(df$Type) 
sapply(1:length(typs),function(ma){points(df[df$Type==typs[ma],"Time"], 
              df[df$Type==typs[ma],"Value2"], 
             col=coly[ma],pch=15+ma ) 
}) 
legend("top",c(mats,typs),pch=c(rep(15,length(mats)),15+1:length(typs)), 
     col=c(palette()[1:length(mats)],coly[1:length(typs)]),horiz=T) 

您将有

enter image description here

+0

看起来不错,谢谢:) – WitheShadow