Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有ggplot2的R中的时间序列_R_Plot_Ggplot2_Time Series - Fatal编程技术网

带有ggplot2的R中的时间序列

带有ggplot2的R中的时间序列,r,plot,ggplot2,time-series,R,Plot,Ggplot2,Time Series,我是ggplot2新手,有一个关于时间序列图的简单问题 我有一个数据集,其中的数据结构如下 Area 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 MIDWEST 10 6 13 14 12 8 10 10 6 9 当数据以这种格式结构化时,如何生成时间序列 使用重塑软件包,我可以将数据更改为: totmidc <- melt(totmidb, id="Area") to

我是ggplot2新手,有一个关于时间序列图的简单问题

我有一个数据集,其中的数据结构如下

      Area 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
  MIDWEST   10    6   13   14   12    8   10   10    6    9
当数据以这种格式结构化时,如何生成时间序列

使用
重塑
软件包,我可以将数据更改为:

totmidc <- melt(totmidb, id="Area")
totmidc

    Area    variable  value
1  MIDWEST     1998    10
2  MIDWEST     1999     6
3  MIDWEST     2000    13
4  MIDWEST     2001    14
5  MIDWEST     2002    12
6  MIDWEST     2003     8
7  MIDWEST     2004    10
8  MIDWEST     2005    10
9  MIDWEST     2006     6
10 MIDWEST     2007     9
但是,是否可以从第一个数据生成时间序列图
对象,其中的列表示年份。

ggplot2给出的错误是什么?以下内容似乎在我的机器上起作用:

Area <-  as.numeric(unlist(strsplit("1998 1999 2000 2001 2002 2003 2004 2005 2006 2007", "\\s+")))
MIDWEST <-as.numeric(unlist(strsplit("10    6   13   14   12    8   10   10    6    9", "\\s+")))

qplot(Area, MIDWEST, geom = "line") + xlab("") + ylab("")

#Or in a dataframe

df <- data.frame(Area, MIDWEST)
qplot(Area, MIDWEST, data = df, geom = "line") + xlab("") + ylab("")
Area我猜“时间序列图”的意思是你想得到条形图而不是折线图

在这种情况下,只需稍微修改代码即可将正确的参数传递给geom_bar()。geom_栏的默认统计是stat_bin,它将计算x标度上类别的频率计数。对于您的数据,您希望覆盖此行为并使用stat_identity

library(ggplot2)

# Recreate data
totmidc <- data.frame(
        Area = rep("MIDWEST", 10),
        variable = 1998:2007,
        value = round(runif(10)*10+1)
)

# Line plot
ggplot(totmidc, aes(variable, value)) + geom_line() + xlab("") + ylab("")

# Bar plot
# Note that the parameter stat="identity" passed to geom_bar()
ggplot(totmidc, aes(x=variable, y=value)) + geom_bar(stat="identity") + xlab("") + ylab("")
库(ggplot2)
#重新创建数据

totmidc感谢您的建议,但我希望用一条线连接数据点。使用条形图在多个时间点上可视化数据的问题是,它可能会导致“过量的字符墨水”(Verzani,2005,第35页)。它应该读取“过量的图表墨水”……无论如何,问题都解决了。
library(ggplot2)

# Recreate data
totmidc <- data.frame(
        Area = rep("MIDWEST", 10),
        variable = 1998:2007,
        value = round(runif(10)*10+1)
)

# Line plot
ggplot(totmidc, aes(variable, value)) + geom_line() + xlab("") + ylab("")

# Bar plot
# Note that the parameter stat="identity" passed to geom_bar()
ggplot(totmidc, aes(x=variable, y=value)) + geom_bar(stat="identity") + xlab("") + ylab("")