如何在以年为x轴和y轴为计数的R中绘制线形图? [英] How to plot line graph in R with years as x-axis and y-axis as count?

查看:129
本文介绍了如何在以年为x轴和y轴为计数的R中绘制线形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数据集:

I have a dataset that looks like this:

Year    AL    AK    AZ    AR    CA    CO
1993    135   153   113   157   718   516
1994    218   154   184   185   845   465
1995    482   846   683   682   863   863

我想绘制随时间变化的折线图,所以x-轴是年份,y轴是计数,每行将是一个状态。
如何获得在x轴上显示的年份?

I want to plot line graphs over time, so x-axis is year, y-axis is the count and each line would be a state. How can I get the year to show up on the x-axis?

我一直在运行:

data <- read.csv("data.csv", header=T)
plot(data$AL, type="l")
par(new=T)
plot(data$AK, type="l")
.....

除上述内容外,x轴为索引,但我希望它为年份。

Except with what I have above, the x-axis is "Index" but I want it to be year.

推荐答案

按时间序列处理数据,解决方案非常简单:

Treat your data as time series and the solution comes very easy:

df <- read.table(text="Year    AL    AK    AZ    AR    CA    CO
1993    135   153   113   157   718   516
1994    218   154   184   185   845   465
1995    482   846   683   682   863   863", header=T)

time.series <- ts(df[, -1], start=1993, end=1995)

plot(time.series, main="Toy example")

产生

如果您只需要一个面板,则:

If you want a single panel, then:

plot(time.series, main="Toy example", plot.type="single", col=1:6)

您可能想阅读?legend 了解如何将图例放入情节中。

You may want to read ?legend to get know how to put legends on plots.

这篇关于如何在以年为x轴和y轴为计数的R中绘制线形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆