[R] formatting dates in axis labels (ggplot2)
Michael Friendly
friendly at yorku.ca
Sat Nov 14 20:49:38 CET 2009
I'm having trouble figuring out how to format Date variables when used
as axis labels in graphs.
The particular case here is an attempt to re-create Nightingale's
coxcomb graph with ggplot2,
where I'd like the months to be labeled as "Mar 1885", "Apr 1885", using
a date format
of "%b %Y" applied to label the dates, or really anything other than
"1885-03-01".
I know the solution has to do with formatting the dates, while
preserving their
status as an ordered factor, but I don't know how to do that.
Here's a subset of the data:
Night1 <-
structure(list(Date = structure(c(-42278, -42248, -42217, -42187,
-42156, -42125, -42095, -42064, -42034, -42003, -41972, -42278,
-42248, -42217, -42187, -42156, -42125, -42095, -42064, -42034,
-42003, -41972, -42278, -42248, -42217, -42187, -42156, -42125,
-42095, -42064, -42034, -42003, -41972), class = "Date"), Cause =
c("Disease",
"Disease", "Disease", "Disease", "Disease", "Disease", "Disease",
"Disease", "Disease", "Disease", "Disease", "Wounds", "Wounds",
"Wounds", "Wounds", "Wounds", "Wounds", "Wounds", "Wounds", "Wounds",
"Wounds", "Wounds", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other"), Deaths = c(1.4,
6.2, 4.7, 150, 328.5, 312.2, 197, 340.6, 631.5, 1022.8, 822.8,
0, 0, 0, 0, 0.4, 32.1, 51.7, 115.8, 41.7, 30.7, 16.3, 7, 4.6,
2.5, 9.6, 11.9, 27.7, 50.1, 42.8, 48, 120, 140.1), Regime = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Before", "After"), class = c("ordered", "factor"
))), .Names = c("Date", "Cause", "Deaths", "Regime"), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L), class = "data.frame")
> str(Night1)
'data.frame': 33 obs. of 4 variables:
$ Date :Class 'Date' num [1:33] -42278 -42248 -42217 -42187 -42156 ...
$ Cause : chr "Disease" "Disease" "Disease" "Disease" ...
$ Deaths: num 1.4 6.2 4.7 150 328.5 ...
$ Regime: Ord.factor w/ 2 levels "Before"<"After": 1 1 1 1 1 1 1 1 1 1 ...
>
Here are a few things I've tried, some of which give errors and others
of which
simply give the wrong graph
library(ggplot2)
cxc1 <- ggplot(Night1, aes(x = factor(Date), y=Deaths, fill = Cause)) +
# do it as a stacked bar chart first
geom_bar(width = 1, position="identity", color="black") +
# set scale so area ~ Deaths
scale_y_sqrt()
# A coxcomb plot = bar chart + polar coordinates
cxc1 + coord_polar(start=3*pi/2) + opts(title="Causes of Mortality in
the Army in the East") + xlab("")
# why doesn't this work?
cxc1 <- cxc1 + scale_x_date(format="%b %Y", major="months")
cxc1
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
OK, I tried formatting Date first, in different ways. Each time, I get
a graphical result, but I
don't know how to use format() for dates to make the result ordered as
normal dates, rather
than as character strings.
Night1$dt1 <- format(Night1$Date, "%b %Y")
cxc1 <- ggplot(Night1, aes(x = factor(dt1), y=Deaths, fill = Cause)) +
geom_bar(width = 1, position="identity", color="black") +
scale_y_sqrt()
cxc1 + coord_polar(start=3*pi/2) + opts(title="Causes of Mortality in
the Army in the East") + xlab("")
-Michael
--
Michael Friendly Email: friendly at yorku.ca
Professor, Psychology Dept.
York University Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html
Toronto, ONT M3J 1P3 CANADA
More information about the R-help
mailing list