如何为具有未命名第一列的数据框的每一行生成时间序列 [英] How to produce time series for each row of a data frame with an unnamed first column

查看:43
本文介绍了如何为具有未命名第一列的数据框的每一行生成时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我确定这是一个相当基本的问题.我有一个数据框,其中包含许多国家/地区 10 年的数据.它看起来像这样.数据框是df.

So this is I'm sure a fairly elementary problem. I have a data frame that has data for 10 years for a bunch of countries. It looks like this. The data frame is df.

                    X2003 X2004 X2005 X2006 X2007 X2008 X2009 X2010 X2011 X2012
Afghanistan         7.321 7.136 6.930 6.702 6.456 6.196 5.928 5.659 5.395 5.141
Albania             2.097 2.004 1.919 1.849 1.796 1.761 1.744 1.741 1.748 1.760
Algeria             2.412 2.448 2.507 2.580 2.656 2.725 2.781 2.817 2.829 2.820
Angola              6.743 6.704 6.657 6.598 6.523 6.434 6.331 6.218 6.099 5.979
Antigua and Barbuda 2.268 2.246 2.224 2.203 2.183 2.164 2.146 2.130 2.115 2.102
Argentina           2.340 2.310 2.286 2.268 2.254 2.241 2.228 2.215 2.201 2.188

第一列是元数据.它没有名字.我想使用 qplot 为每一行绘制时间序列.类似于以下命令:

The first column is metadata. It hasn't got a name. I'd like to use qplot to plot time series for each of the rows. Something like the following command:

library(ggplot2)
qplot (data = df, binwidth = 1, geom="freqpoly") but I get the following error

Error: stat_bin requires the following missing aesthetics: x. 

我想设置 x = 第一列,但我在该列上没有名称.我是否必须创建第一列国家/地区名称?如果是这样,我该怎么做?似乎应该有更简单的方法.抱歉,这太初级了.

I would like to set x = first column but I don't have a name on that column. Do I have to create a first column of country names? If so, how do I do that? Seems like there should be an easier way. Sorry if this is so elementary.

推荐答案

不确定您需要什么,也许是这样的?

Not sure what you need, maybe something like this?

library(reshape2)
library(ggplot2)
df$metadata <- row.names(df)
df <- melt(df, "metadata")
ggplot(df, aes(variable, value, group = metadata, color = metadata)) +
  geom_line()

这篇关于如何为具有未命名第一列的数据框的每一行生成时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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