1
votes

I'm trying to graph the data below using ggplot2.

ggplot(dailyHDD, aes(x=Date, y=cumHDD, colour=WinterID, group=WinterID)) + 
    geom_line(linetype="dashed", size=1) +
    scale_x_date(labels=date_format("%B"), breaks=date_breaks("month"))

Data structure:

structure(list(SiteID = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "NW_SB", class = "factor"), 
    SubstrateID = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "B", class = "factor"), 
    WinterID = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("2002_2003", 
    "2003_2004", "2004_2005", "2005_2006", "2006_2007", "2007_2008"
    ), class = "factor"), Date = structure(c(11992, 11993, 11994, 
    11995, 11996), class = "Date"), HeatingDegreeDay = c(0, 0, 
    0, 0, 0), cumHDD = c(0, 0, 0, 0, 0), Month = structure(c(1L, 
    1L, 1L, 1L, 1L), .Label = c("11", "12", "01", "02", "03"), class = c("ordered", 
    "factor"))), .Names = c("SiteID", "SubstrateID", "WinterID", 
"Date", "HeatingDegreeDay", "cumHDD", "Month"), row.names = c(NA, 
5L), class = "data.frame")

My data:

    SiteID SubstrateID  WinterID       Date HeatingDegreeDay     cumHDD Month
 15  NW_SB           B 2002_2003 2002-11-15        0.0000000  0.0000000    11
 16  NW_SB           B 2002_2003 2002-11-16        0.0000000  0.0000000    11
 17  NW_SB           B 2002_2003 2002-11-17        0.0000000  0.0000000    11
 18  NW_SB           B 2002_2003 2002-11-18        0.0000000  0.0000000    11
 19  NW_SB           B 2002_2003 2002-11-19        0.3724242  0.3724242    11
 20  NW_SB           B 2002_2003 2002-11-20        0.0000000  0.3724242    11
 21  NW_SB           B 2002_2003 2002-11-21        0.0000000  0.3724242    11
 22  NW_SB           B 2002_2003 2002-11-22        0.0000000  0.3724242    11
 23  NW_SB           B 2002_2003 2002-11-23        0.2773077  0.6497319    11
 24  NW_SB           B 2002_2003 2002-11-24        0.2679310  0.9176630    11
 25  NW_SB           B 2002_2003 2002-11-25        0.0000000  0.9176630    11
 26  NW_SB           B 2002_2003 2002-11-26        0.0000000  0.9176630    11
 27  NW_SB           B 2002_2003 2002-11-27        0.0000000  0.9176630    11
 28  NW_SB           B 2002_2003 2002-11-28        0.0000000  0.9176630    11
 29  NW_SB           B 2002_2003 2002-11-29        0.2063889  1.1240519    11
 30  NW_SB           B 2002_2003 2002-11-30        1.1830556  2.3071074    11
 31  NW_SB           B 2002_2003 2002-12-01        0.1811111  2.4882185    12
 32  NW_SB           B 2002_2003 2002-12-02        1.0348276  3.5230461    12
 33  NW_SB           B 2002_2003 2002-12-03        1.0064286  4.5294747    12
 34  NW_SB           B 2002_2003 2002-12-04        0.6238462  5.1533208    12
 35  NW_SB           B 2002_2003 2002-12-05        0.1628571  5.3161780    12
 36  NW_SB           B 2002_2003 2002-12-06        0.0000000  5.3161780    12
 37  NW_SB           B 2002_2003 2002-12-07        0.8393103  6.1554883    12
 38  NW_SB           B 2002_2003 2002-12-08        0.1448000  6.3002883    12
 39  NW_SB           B 2002_2003 2002-12-09        0.0000000  6.3002883    12
 40  NW_SB           B 2002_2003 2002-12-10        1.0744444  7.3747328    12
 41  NW_SB           B 2002_2003 2002-12-11        0.0000000  7.3747328    12
 42  NW_SB           B 2002_2003 2002-12-12        0.2100000  7.5847328    12
 43  NW_SB           B 2002_2003 2002-12-13        1.5238889  9.1086217    12
 44  NW_SB           B 2002_2003 2002-12-14        2.2277778 11.3363994    12
 45  NW_SB           B 2002_2003 2002-12-15        4.1022222 15.4386217    12
 46  NW_SB           B 2002_2003 2002-12-16        4.5455556 19.9841772    12
 47  NW_SB           B 2002_2003 2002-12-17        4.3391667 24.3233439    12
 48  NW_SB           B 2002_2003 2002-12-18        3.3436111 27.6669550    12
 49  NW_SB           B 2002_2003 2002-12-19        1.9597222 29.6266772    12
 50  NW_SB           B 2002_2003 2002-12-20        0.5586111 30.1852883    12
...    
150  NW_SB           B 2002_2003 2003-03-30       0.00000000 150.0560881    03
151  NW_SB           B 2002_2003 2003-03-31       0.00000000 150.0560881    03
152  NW_SB           B 2003_2004 2003-11-15       0.00000000   0.0000000    11

ggplot plots chronologically and I end up with a line for 2002-2003 that raises vertically to value 150, and the same for each consecutive year. Instead I want the plot to show a line that rises diagonally for each year, starting at 0 on the y-axis, and November on the x-axis, and rises over time (Date) to its end cumHDD value.

enter image description here

I'm new to R and to SO. Thank you @Roman Luštrik for posting my plot. Thanks for your help.

3
It's because you're plotting against the variable Month and not Date.Señor O

3 Answers

1
votes

This is how I solved my problem. I formatted my Date field. I added a MonthDay field, then formatted it to character and back to factor with unique levels and order.

# Format Date
dailyHDD$Date <- as.Date(dailyHDD$Date, order = "ymd")
# Add MonthDay column dailyHDD data frames for graphing purposes.
dailyHDD$MonthDay <- format(dailyHDD$Date, "%m%d") 
# Format Month field as factor so that month order is retained on x-axis of graph
dailyHDD$MonthDay <- as.character(dailyHDD$MonthDay)
dailyHDD$MonthDay <- factor(dailyHDD$MonthDay, levels=unique(dailyHDD$MonthDay), ordered=TRUE)

Then I plotted my graph. All of the MonthDay, starting at 1101 (November 1) and ending at 0331 (March 31), appeared as labels. To fix this, I added breaks using 1101 for "November" and added labels, e.g. "November".

ggplot(dailyHDD, aes(x=MonthDay, y=cumHDD, colour=WinterID, group=WinterID)) + 
  geom_line(linetype="dashed", size=1) +
  scale_x_discrete(breaks=c("1101", "1201", "0101", "0201", "0301"),
               labels=c("November", "December", "January", "February", "March")) +
  labs(x= "Date", y="Cumulative Degree-days")
1
votes

This doesn't answer your question regarding ggplot, but here's a simple way to plot time series. You might want to look into the xts package further as it has lots of great ways to manipulate time series.

require(xts)

df <- as.xts(df, order.by=df$Date)

plot(df$cumHDD, type='l')
0
votes

One solution (kind of a hack) is to subtract 365 from each date for each year it is past the original WinterID. Assuming WinterID is in order, you can do:

df$NormalizedDate = df$Date - (365 * (as.numeric(df$WinterID) - 1))

ggplot(df, aes(NoramlizedDate, cumHDD, color=WinterID)) + geom_line()