2015-08-13 20 views
1

我有數據框,我想先傳遞兩列行+變量名稱到圖例。使用行名在R中添加圖例

df內部我有一組數據集,其中他們用ah之間的字母分組。

我想成功的事情是,像78_256_DQ0_a78_256_DQ1_a78_256_DQ2_a到傳說a等其他羣體。 我不知道如何將此格式傳遞給ggplot。 任何幫助將不勝感激。

可以說我有這樣的數據框;

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)) 
state <- rep(rep(c("Group 1","Group 2"),each=5),times=6) 
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 
***************************************************** 

此代碼爲繪製df

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

enter image description here

回答

2

添加lty = variable你的美感,就像這樣:

ggplot(df_new, aes(y = value, x = V, lty = variable, colour = No)) + 
    geom_point() + 
    geom_line() 

會給你DQ0DQ1DQ2單獨的線路。

enter image description here

+0

是的,謝謝你。怎麼處理行名?是可能的:) – Alexander

+0

@aoronbarlow你的數據框沒有rownames。你是什​​麼意思? –