简体   繁体   English

如何绘制包含多个观察对象的时间序列表

[英]How to plot a time-series table containing several observation objects

First, I'm a kind of a greenhorn in R. Unfortunately I couldn't find an existing tag which is related to my problem. 首先,我在R中有点像个绿角。不幸的是,我找不到与我的问题相关的现有标签。 I have several data.frames of the following structure: 我有以下结构的几个data.frames:

Lines: City1, City2, City3, (...) 行:City1,City2,City3,(...)

Columns: Migration-Flow 2004, Migration-Flow 2005, Migration-Flow 2006 (...) 列:Migration-Flow 2004,Migration-Flow 2005,Migration-Flow 2006(...)

City    Migration-Flow 2004    Migration-Flow 2005    Migration-Flow 2006
----    -------------------    -------------------    -------------------
City1   5920                   1339                   -3394
City2   3001                   4129                   4430
...

My lines (not my columns) representing a times series of each city. 我的行(而不是我的列)代表每个城市的时间序列。 For instance, data.frame[,2] represents the time series of City1 between 2004 and 2010. 例如,data.frame [,2]表示2004年至2010年之间的City1时间序列。

What I want to do is, to plot every line (Migration-Flow of each city from 2004 to 2010) in one single chart in order to compare them. 我想做的是在一张图表中绘制每条线(2004年至2010年每个城市的迁移流量)以进行比较。 Are there any ways to do this? 有什么方法可以做到这一点?

The brute-force non-elegant method (but why not, it's short): 蛮力非优雅的方法(但是,为什么呢,它很简短):

# Generate artificial data
flow = matrix(runif(100,1000,2000),nrow=10)
d = data.frame(City=paste0("City",1:10),flow)

# Brute force
plot(unlist(d[1,-1]), type = "l")
for (i in 2:nrow(d)) lines(unlist(d[i,-1]),col=i)

Note that in your example data.frame[2,] is for City 1. 请注意,在您的示例中data.frame [2,]适用于城市1。

Using the plot command, you can generate a plot from the first row of your data. 使用plot命令,可以从数据的第一行生成图。 Then, using lines , you can add a new line to your plot for each new row. 然后,使用lines ,您可以为绘图中的每一行添加一条新线。 A simple for loop would make this process relatively simple. 一个简单的for循环将使此过程相对简单。 A trivial example is below: 一个简单的例子如下:

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])
}

This produces the output below: 这将产生以下输出:

绘图输出

Using the legend command you could produce a legend, and using colorRamp would let you have the colors dynamically change based on the number of rows. 使用legend命令可以生成一个图例,而使用colorRamp可以使颜色根据行数而动态变化。 Arguments to xlab in plot would allow you to make the x-axis labels more complex. plot使用xlab参数可以使x轴标签更复杂。 While this code is simple and relies on a specific number of rows, you could easily generalize it to work on more varied data. 尽管此代码很简单,并且依赖于特定的行数,但是您可以轻松地将其通用化,以处理更多不同的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM