0

我觉得我应该能够通过调用来完成这一任务,lines()但我有以下内容:

data <- read.csv(...)
set1 <- subset(data, level='one')
set2 <- subset(data, level='two')
set3 <- subset(data, level='three')
plot(x=data$year, y=data$output)
lines(x=set1$year, y=set1$output, col='red')
lines(x=set2$year, y=set2$output, col='blue')
lines(x=set3$year, y=set3$output, col='green')

这感觉就像一个真正的 101 应用程序plotlines我只是想让它更有效率,特别是考虑到我最终会有 N 次subset调用。

4

1 回答 1

1

你可以只使用排序的循环

colors <- c('red', 'blue', 'green')
levels <- c('one', 'two', 'three')

# make sure you made a new plot up to this point and that its limits are set correctly

for (i in 1:length(colors)) {
    d <- subset(data, level=levels[i])
    lines(x=d$year, y=d$output, col=colors[i])
)

这可以很好地扩展到大 N。您还可以使用例如生成颜色列表colors <- rainbow(N)

如果您想在没有循环的情况下执行此操作,您可以查看matplot哪些matlines矩阵的列相互对应。但是为此,您的所有子集都必须具有相同的长度,我认为您的情况并非如此。

于 2014-11-14T21:33:15.870 回答