使用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