2014-03-03 24 views
1

我非常害怕我一直在嘗試發明熱水,但是我想根據主題編號連接下圖中的點。在這種情況下,受試者是老鼠,他們分爲三個治療組,他們的體重測量三次。如何根據主題編號在普通情節中連接點

有沒有一種方法可以輕鬆連接基於鼠數的點數?

的數據看起來是這樣的(第20行):

Rat Group Measurement Weight 
1 1  1   M0  57 
2 2  1   M0  60 
3 3  1   M0  52 
4 4  1   M0  49 
5 5  1   M0  56 
6 6  1   M0  46 
7 7  1   M0  51 
8 8  1   M0  63 
9 9  1   M0  49 
10 10  1   M0  57 
11 11  2   M0  61 
12 12  2   M0  59 
13 13  2   M0  53 
14 14  2   M0  59 
15 15  2   M0  51 
16 16  2   M0  51 
17 17  2   M0  56 
18 18  2   M0  58 
19 19  2   M0  46 
20 20  2   M0  53 

而且我說的圖表是這一個:

enter image description here

對此我用這個代碼做:

par(mfrow=c(1,3)) 
plot(data=data_long[data_long$Group==1,], Weight ~ as.numeric(Measurement), 
    ylim=c(0,200), pch=20, xlab="Measurement", main="Treatment group 1") 
par(new=TRUE) 
grid() 
plot(data=data_long[data_long$Group==2,], Weight ~ as.numeric(Measurement), 
    ylim=c(0,200), pch=20, xlab="Measurement", main="Treatment group 2") 
grid() 
plot(data=data_long[data_long$Group==3,], Weight ~ as.numeric(Measurement), 
    ylim=c(0,200), pch=20, xlab="Measurement", main="Treatment group 3") 
grid() 

我該如何讓R連接點基於老鼠的號碼?

謝謝!

編輯:請dput:

structure(list(Rat = 1:20, Group = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L 
), .Label = c("1", "2", "3"), class = "factor"), Measurement = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("M0", "M1", "M2", "M3", "M4"), class = "factor"), 
    Weight = c(57L, 60L, 52L, 49L, 56L, 46L, 51L, 63L, 49L, 57L, 
    61L, 59L, 53L, 59L, 51L, 51L, 56L, 58L, 46L, 53L)), .Names = c("Rat", 
"Group", "Measurement", "Weight"), row.names = c(NA, 20L), class = "data.frame") 
+0

您可以提供您的數據'dput(頭(data_long變得單調而乏味,20 ))'作爲你的文章的編輯? – Thomas

+0

重塑您的數據並使用'matlines'。 – Roland

+0

所以你想連接屬於'data $ rat == j'的所有點?然後通過鼠編號選擇當前'x'和'y'數據的子集。這基本上是羅蘭所建議的。 –

回答

3

我推薦使用ggplot2,因爲它可以在基地圖形

# sample dataset 
set.seed(12345) 
data <- data.frame(
    rat = factor(rep(1:5, times=6)), 
    group = factor(rep(1:2, each=15)), 
    measurement = factor(rep(1:3, each=5, times=2)), 
    weight = runif(30, min=20, max=60)) 

# require(ggplot2) 
ggplot(data=data, aes(x=measurement, y=weight, col=group, group=group:rat)) + 
geom_point() + geom_line() + facet_wrap(~group, ncol=2) 

enter image description here

1

如建議被大家,你應該使用ggplot,或者如果您使用基本圖形,使用一些for循環。因爲我還不知道ggplot,所以我用for循環作爲結果。

它仍然比我們奮鬥了教授走線槽與SAS比較容易的方式,所以我不介意的for循環...

下面是代碼。謝謝大家的幫助。

enter image description here

par(mfrow=c(2,3)) 
lapply(1:3, function(g) { 
    plot(data=data_long[data_long$Group==g,], Weight ~ as.numeric(Measurement), 
     ylim=c(0,200), pch=20, xlab="Measurement", 
     main=paste("Treatment group",g)) 
    for(j in 1:max(data_long$Rat)){ 
     if(data_long[j,]$Group==g){ 
      lines(data_long[data_long$Rat==j,]$Measurement, 
       data_long[data_long$Rat==j,]$Weight) 
     } 
    } 
    grid() 
}) 
plot(data=data_long[data_long$Group==1,], Weight ~ Measurement, 
    ylim=c(0,200), pch=20, xlab="Measurement", main="Treatment group 1") 
grid() 
plot(data=data_long[data_long$Group==2,], Weight ~ Measurement, 
    ylim=c(0,200), pch=20, xlab="Measurement", main="Treatment group 2") 
grid() 
plot(data=data_long[data_long$Group==3,], Weight ~ Measurement, 
    ylim=c(0,200), pch=20, xlab="Measurement", main="Treatment group 3") 
grid() 
+1

我將發佈簡化的代碼。確保它仍然有效。如果沒有,還原我的編輯。 – Thomas

+0

有趣,thx! –

+0

@Thomas,這種方法似乎有問題。 –