2016-07-29 74 views
1

我希望有一個線圖,其中valuevar繪製成的expt一個功能,每一級行:GGPLOT2:因子與geom_line x軸不起作用

這裏是我的數據:

lines <- " 
expt,var,value 
1,none.p,0.183065327746799 
2,none.p,0.254234138384241 
3,none.p,0.376477571234912 
1,male.p,-1.58289835719949 
2,male.p,-1.98591548366901 
3,male.p,-2.02814824729229 
1,neutral.p,-2.01490302054226 
2,neutral.p,-1.88178562088577 
3,neutral.p,-1.68089687641625 
1,female.p,-3.27294304613848 
2,female.p,-3.07711187982237 
3,female.p,-2.89652562347054 
1,both.p,-2.40011011312792 
2,both.p,-2.24495598015741 
3,both.p,-2.78501124223834" 
con <- textConnection(lines) 
data <- read.csv(con) 
close(con) 

expt是一個因素:按預期工作

data$expt <- factor(data$expt) 

的一切,當我使用geom_point

ggplot(data, aes(expt, value, colour=var)) + geom_point() 

但是當我使用geom_line

ggplot(data, aes(expt, value, colour=var)) + geom_line() 

我收到以下錯誤消息

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? 

和一個空的情節。當expt是數字時,它可以工作,但我更喜歡使用一個因子,因爲這給了我x軸上的正確標籤。這裏有什麼問題?我發現它非常直觀地表明這與點有關,但與線無關。

回答

5

您除了需要使用groupcolor ...

ggplot(data, aes(expt, value, group=var, color=var)) + geom_line() 

給了我這樣的輸出:

enter image description here

+0

謝謝。我現在明白爲什麼這是正確的解決方案,但我不明白爲什麼它在expt是數字時有效。似乎不應該。此外,錯誤信息可能不正確。 – tmalsburg

+1

@tmalsburg這裏有一些解釋:[plotting-lines-and-the-group-aesthetic-in-ggplot2](http://stackoverflow.com/questions/10357768/plotting-lines-and-the-group-aesthetic-in -ggplot2) –