开发者

Time Series in R with ggplot2

开发者 https://www.devze.com 2023-02-10 04:46 出处:网络
I\'m a ggplot2 newbie and have a rather simple question regarding time-series plots. I have a data set in which the data is structured as follo开发者_JS百科ws.

I'm a ggplot2 newbie and have a rather simple question regarding time-series plots.

I have a data set in which the data is structured as follo开发者_JS百科ws.

      Area 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
  MIDWEST   10    6   13   14   12    8   10   10    6    9

How do I generate a time series when the data is structured in this format.

With the reshape package, I could just alter the data to look like:

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

Then run the following code to get the desired plot.

ggplot(totmidc, aes(Variable, Value)) + geom_line() + xlab("") + ylab("")

However, is it possible to generate a time series plot from the first object in which the columns represent the years.


What is the error that ggplot2 gives you? The following seems to work on my machine:

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("")

You may also want to check out the ggplot2 website for details on scale_date et al.


I am guessing that with "time series plot" you mean you want to get a bar chart instead of a line chart?

In that case, you have to modify your code only slightly to pass the correct parameters to geom_bar(). The geom_bar default stat is stat_bin, which will calculate a frequency count of your categories on the x-scale. With your data you want to override this behaviour and use 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("")

This produces the following bar plot:

Time Series in R with ggplot2

0

精彩评论

暂无评论...
验证码 换一张
取 消