[R] Time Series in R with ggplot2
Ista Zahn
izahn at psych.rochester.edu
Sat Feb 12 00:28:02 CET 2011
Hi,
You probably have Year stored as a factor. See below.
totsoc <- structure(list(Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "SOUTH", class = "factor"), Year = 1998:2007,
Value = c(29L, 20L, 32L, 29L, 25L, 28L, 27L, 28L, 22L, 31L
)), .Names = c("Location", "Year", "Value"), class = "data.frame",
row.names = c(NA,
-10L))
qplot(Year, Value, data=totsoc, geom="line")#works as expected
ggplot(totsoc, aes(x=Year, y=Value)) + geom_line()#same
##convert year to a factor
dat <- totsoc
dat$Year <- factor(dat$Year)
qplot(Year, Value, data=dat, geom="line")## this now reproduces your problem
ggplot(dat, aes(x=Year, y=Value)) + geom_line() ## same
## Solutions: 1) convert year to numeric, or 2) use group=1 as shown
below, or 3) convert year to date class (this always gives me problems
so I don't show an example).
qplot(Year, Value, data=dat, geom="line", group=1)
ggplot(dat, aes(x=Year, y=Value)) + geom_line(aes(group=1))
Best,
Ista
On Fri, Feb 11, 2011 at 5:29 PM, <info at mathewanalytics.com> wrote:
>
>
> Hi Folks,
>
> First, the important information.
>
>> sessionInfo()
> R version 2.12.1 (2010-12-16)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> Second, my problem.
>
> I have a series of data sets comprised in the following format.
>
>> totsoc
> Location Year Value
> 1 SOUTH 1998 29
> 2 SOUTH 1999 20
> 3 SOUTH 2000 32
> 4 SOUTH 2001 29
> 5 SOUTH 2002 25
> 6 SOUTH 2003 28
> 7 SOUTH 2004 27
> 8 SOUTH 2005 28
> 9 SOUTH 2006 22
> 10 SOUTH 2007 31
>
> In order to generate a time series plot in ggplot2, I ran the following
> code.
>
> qplot(Year, Value, data=totsoc, geom="line")
>
> ggplot(totsoc, aes(x=Year, y=Value)) + geom_line()
>
> However, neither command acctually produces a plot with lines connecting
> the
> data points. I get a blank window with the general gray background and the
> x
> and y axis. The strange thing is that ggplot2 gives me the appropriate
> output
> when I use "bar" or "point". For example, these commands work.
>
> ggplot(totsoc, aes(Year, Value)) + geom_point()
>
> qplot(Year, Value, data=totsoc, geom="point")
>
> I also tried to generate some sample data, and that worked. However,
> I'm not sure why these same commands aren't working on the earlier data
> set.
> Here is the sample data I was working with.
>
> df <- data.frame(one=c(3,8,5,4,2), two=c("KS","MO","KS","CA","IA"),
> three=c(2001:2005))
> qplot(three, one, data=df, geom="line")
>
>
> Can anyone please help?
>
> Thank You,
> A. Mathew
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
More information about the R-help
mailing list