如何根据主题编号在普通图中连接点

时间:2014-03-03 14:36:20

标签: r

我非常害怕我一直试图发明热水,但我想根据主题编号连接下图中的点。在这种情况下,受试者是大鼠,他们分成三个治疗组,并且他们的体重测量三次。

有没有办法可以根据老鼠的数量轻松连接点?

数据看起来像这样(前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连接点?

谢谢!

编辑:请求输入:

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

2 个答案:

答案 0 :(得分: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 :(得分: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()