【发布时间】:2015-08-14 07:16:37
【问题描述】:
我有数据框,我想传递前两列行和 用于创建图例的变量列名。
在df 内部,我有一组数据集,它们用从a 到h 的字母分组。特别是,我想将AC&AR 列行作为名称与DQ0:DQ2 变量组合传递,它们应该以该格式显示在图例中。
对于数据组a,类似于 78_256_DQ0、78_256_DQ1 和 78_256_DQ2
和df 中的letters 的其余部分相同。
我的可复制df 像这样;
df <- do.call(rbind,lapply(1,function(x){
AC <- as.character(rep(rep(c(78,110),each=10),times=3))
AR <- as.character(rep(rep(c(256,320,384),each=20),times=1))
V <- rep(c(seq(2,40,length.out=5),seq(-2,-40,length.out=5)),times=2)
DQ0 = sort(replicate(6, runif(10,0.001:1)))
DQ1 = sort(replicate(6, runif(10,0.001:1)))
DQ2 = sort(replicate(6, runif(10,0.001:1)))
No = c(replicate(1,rep(letters[1:6],each=10)))
data.frame(AC,AR,V,DQ0,DQ1,DQ2,No)
}))
头(df)
AC AR V DQ0 DQ1 DQ2 No
1 78 256 2.0 0.003944916 0.00902776 0.00228837 a
2 78 256 11.5 0.006629239 0.01739512 0.01649540 a
3 78 256 21.0 0.048515226 0.02034436 0.04525160 a
4 78 256 30.5 0.079483625 0.04346118 0.04778420 a
5 78 256 40.0 0.099462310 0.04430493 0.05086738 a
6 78 256 -2.0 0.103686255 0.04440260 0.09931459 a
*****************************************************
library(reshape2)
df_new <- melt(df,id=c("V","No"),measure=c("DQ0","DQ1","DQ2"))
library(ggplot2)
ggplot(df_new,aes(y=value,x=V,group=No,colour=No))+
geom_point()+
geom_line()
更新
在@...回答之后,我取得了一点进步。他的解决方案部分没问题。因为当我们melt名字时
df$names <- interaction(df$AC,df$AR,names(df)[4:6])
df_new <- melt(df,id=c("V","No","names1"),measure=c("DQ0","DQ1","DQ2"))
此命令为 a 到 h 的每个组绘制 4 行。
输出变成这样;
头(df)
AC AR V DQ0 DQ1 DQ2 No names
1 78 256 2.0 0.002576547 0.04294134 0.008302918 a 78.256.DQ0
2 78 256 11.5 0.010150299 0.04570650 0.011749370 a 78.256.DQ1
3 78 256 21.0 0.012540026 0.06977744 0.013887357 a 78.256.DQ2
4 78 256 30.5 0.036532977 0.11460343 0.071172301 a 78.256.DQ0
5 78 256 40.0 0.042801967 0.11518191 0.073756228 a 78.256.DQ1
6 78 256 -2.0 0.043275144 0.13033194 0.076569977 a 78.256.DQ2
**************************************************************
并修改绘图命令
ggplot(df_new,aes(y=value,x=V,lty=variable,colour=names))+
geom_point()+
geom_line()
我更喜欢的输出格式是我可以引用每个组内的所有 DQ0、DQ1 和 DQ2 行。有什么建议吗?
【问题讨论】: