formatting dates in axis labels (ggplot2)
hadley wickham wrote:
Hi Michael,
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.
Dates are, by their very nature, ordered so you don't need to keep them as an ordered factor. If you don't do that you'll find that the scale_date formatting options will work.
I would have thought so, but I get an error unless I use
aes(x=factor(Date)...) or aes(x=ordered(Date)...) in the original
call to ggplot:
> cxc1 <- ggplot(Night1, aes(x = 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("")
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Error in pmin(y, 0) : object 'y' not found
I apologize for including attachments, but I can't get to a server where
I can place the results as links.
If I use aes(x=factor(Date)...), I get an appropriate graph, except that
the dates appear as "1885-03-01". [See attached: night1-factor.png]
With aes(x=ordered(Date)...), the same code gives only a bar chart
[night1-ordered.png]
If I format the date first, I get a result where the dates are ordered
alphabetically [night1-formatted.png]
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("")
For completeness, here is the data I was using:
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")
-Michael
-------------- next part --------------
A non-text attachment was scrubbed...
Name: night1-factor.png
Type: image/png
Size: 39591 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091121/76b0c976/attachment-0006.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: night1-ordered.png
Type: image/png
Size: 27564 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091121/76b0c976/attachment-0007.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: night1-formatted.png
Type: image/png
Size: 39742 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091121/76b0c976/attachment-0008.png>