0

我有以下数据:

Animal  MY  Age
1   17.03672067 1
1   17.00833641 2
1   16.97995215 3
1   16.95156788 4
1   16.92318362 5
1   16.88157748 6
2   16.83997133 2
2   16.79836519 3
2   16.75675905 4
2   16.7151529  5
2   16.67354676 6
2   16.63194062 7
3   16.59033447 1
3   16.54872833 2
3   16.50712219 3
3   16.46551604 4
3   16.4239099  5
3   16.38230376 6
4   16.34069761 1
4   16.29909147 2
4   16.25748533 3
4   16.21587918 4
4   16.17427304 5
4   16.1326669  6

我想为每只动物绘制 MY vs Age 之间的散点图。我用这个功能

  plot(memo$MY[memo$Animal=="1223100747"]~memo$Age[memo$Animal=="1223100747"]).

如果我现在想为其他动物添加相同的情节(MY vs Age),我只需要使用 function: lines。但是,由于我有大约 200 只动物,我不想手动执行 100 次。我的问题是:如何通过一个函数绘制这些不同的动物?,而不是使用lineslines.... lines

问候, 芳

4

2 回答 2

0

您可以使用by例如:

by(memo,memo$Animal,FUN=function(x) plot(x$MY~x$Age))
于 2013-05-24T09:28:18.973 回答
0

如果你想使用基础 R,你可以使用循环或 matplot,但我建议你使用包 ggplot2。

DF <- read.table(text="Animal  MY  Age
1   17.03672067 1
1   17.00833641 2
1   16.97995215 3
1   16.95156788 4
1   16.92318362 5
1   16.88157748 6
2   16.83997133 2
2   16.79836519 3
2   16.75675905 4
2   16.7151529  5
2   16.67354676 6
2   16.63194062 7
3   16.59033447 1
3   16.54872833 2
3   16.50712219 3
3   16.46551604 4
3   16.4239099  5
3   16.38230376 6
4   16.34069761 1
4   16.29909147 2
4   16.25748533 3
4   16.21587918 4
4   16.17427304 5
4   16.1326669  6",header=TRUE)

library(ggplot2)
DF$Animal <- factor(DF$Animal)

p1 <- ggplot(DF,aes(x=MY,y=Age,colour=Animal)) + geom_line()
print(p1)
于 2013-05-24T09:28:23.600 回答