simple geom="line" not working with dates

642 views
Skip to first unread message

Henry Drexler

unread,
Oct 20, 2011, 10:27:12 AM10/20/11
to ggplot2
this works:
a <- data.frame(series=c(1,2,3,4),score=c(2,7,3,4))
qplot(series,score,data=a,geom=c("line","point"))

This time with dates -
this does not work(point does, line doesn't):
c <-
data.frame(series=c('2011-01-01','2011-01-02','2011-01-03','2011-01-04'),score=c(2,7,3,4))
qplot(series,score,data=c,geom=c("line","point"))

I have read up in http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf
"Date and Time classes in R"

as a result of that I created a csv file named heavy.csv with the
content of:

series,height
2011-01-01,5.4
2011-01-02,5.7
2011-01-03,5.3
2011-01-04,5.9
2011-01-05,6.3
2011-01-06,5.7

I read it into r with
df <- read.csv("heavy.csv", header = TRUE)
as.Date(as.character(df$series), "%Y-%m-%d") #this part coming from
the afore mentioned pdf

no success.

Any ideas on what I am missing to get dates imported/working properly
to just do a simple geom="line" with its data?

Dennis Murphy

unread,
Oct 20, 2011, 4:18:54 PM10/20/11
to Henry Drexler, ggplot2
Hi:

You need to tell ggplot2 that the horizontal axis is a date variable.
Both of the following work; I read your data in as a data frame named
df with respective variables series and height, per your csv file.
(BTW, c is not an inspired choice of a data frame name since it is the
same as one of the most commonly used functions in the language.)

ggplot(df, aes(x = series, y = height)) + geom_point() + geom_line() +
scale_x_date()
qplot(series, height, data=df, geom=c("line","point")) + scale_x_date()

HTH,
Dennis

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Henry Drexler

unread,
Oct 20, 2011, 4:34:40 PM10/20/11
to Dennis Murphy, ggplot2
Dennis, thank you for your comments.

If I understand you correctly, doing the following should work(for the csv file):

df <- read.csv("heavy.csv", header = TRUE)
as.Date(as.character(df$series), "%Y-%m-%d")

then

ggplot(df, aes(x = series, y = height)) + geom_point() + geom_line() +
   scale_x_date()

or

qplot(series, height, data=df, geom=c("line","point")) + scale_x_date()


unfortunately neither of those worked for me - perhaps do I need to load another library?

Also, I was using c as that is what I have always seen done - I thought that meant you were using a series of values.  can any letter work there then?

Dennis Murphy

unread,
Oct 20, 2011, 8:24:53 PM10/20/11
to Henry Drexler, ggplot2
You need to change the class of df$series from factor to Date; you did
everything but the assignment:

df$series <- as.Date(as.character(df$series), "%Y-%m-%d")
str(df) # verify that the class of series is Date rather than factor

Now plot.

HTH,
Dennis

Henry Drexler

unread,
Oct 21, 2011, 6:51:18 AM10/21/11
to Dennis Murphy, ggplot2
I know that was a bit of hand holding, but now it is perfectly clear - thank you for your time and assistance.
Reply all
Reply to author
Forward
0 new messages