使用plot
命令,可以从数据的第一行生成一个图。然后,使用lines
,您可以为每个新行添加一条新图。一个简单的for
循环会使这个过程相对简单。一个简单的例子是如下:
set.seed(519310)
testdata <- data.frame(City=c("Trenton","New Dehli","Kabul","New York","Santa Fe","Berlin","Oslo"),
Migration.2004=sample(x = -1000:1000,size = 7,replace = TRUE),
Migration.2005=sample(x = -1000:1000,size = 7,replace = TRUE),
Migration.2006=sample(x = -1000:1000,size = 7,replace = TRUE),
Migration.2007=sample(x = -1000:1000,size = 7,replace = TRUE),
Migration.2008=sample(x = -1000:1000,size = 7,replace = TRUE),
Migration.2009=sample(x = -1000:1000,size = 7,replace = TRUE),
Migration.2010=sample(x = -1000:1000,size = 7,replace = TRUE))
plot(x = 1:(ncol(testdata)-1),
y = testdata[1,2:8],
type = "l",
ylim = c(-1000,1000))
colors <- c("red","blue","green","yellow","purple","orange","dark blue")
for(x in 2:nrow(testdata)) {
lines(x = 1:(ncol(testdata)-1),y = testdata[x,2:8],type = "l",col = colors[x])
}
这将产生以下的输出:
使用legend
命令,你可以制作一个传奇,并使用colorRamp
会让你有颜色动态更改在行数上。 xlab
在plot
中的参数将允许您使x轴标签更加复杂。虽然这段代码很简单并且依赖于特定数量的行,但您可以轻松将其推广到更多不同的数据。
我编辑了你的问题来表达你的数据看起来像。请确认我的编辑已正确表示。 – TARehman