3

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 follows.

      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.

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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:

enter image description here

2 Comments

thanks for the suggestion, but I was looking to connect the data points with a line. The problem with using barplots to visualize data over several points in time is that it can result in "excessive amounts of char ink" (Verzani, 2005, p.35)
it's supposed to read "excessive amounts of chart ink"....in any case, the problem is solved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.