Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add abline in ggplot2 with x-axis as year?

Tags:

date

plot

r

ggplot2

I am trying to make a temperature trend plot using ggplot2. i could not plot abline with given slope and intercept (they are the result of Sen Estimate for trend calculation).

df is subset of my data.

df<- structure(list(Mean_Tmin = c(-6.85888797419416, -6.61640313272608, 
-4.10521557474695, -3.33048575952967, -4.85158854541956, -6.90958783702363, 
-5.4738307882025, -5.50757613208475, -3.34384859553843, -4.79079906097484, 
-4.64407818570133, -4.17247075814977, -4.00324802670715, -2.92271555321199, 
-4.26302362769172, -4.05299360203852, -3.65186880762282, -2.6784292470159, 
-3.55928476504212, -3.26383609036539, -3.75455004498325, -4.01641178860892, 
-4.95576810887431, -3.85043378628423, -3.39114296653151, -4.62589589895125, 
-2.62611368991667, -3.60484580346817, -3.55386283157491, -3.76902382151618, 
-3.05841501472943, -1.77644594829943, -4.01934355211525, -2.35357265559614, 
-3.63012525456073, -4.61818077798782, -2.78669513101182, -1.87421430381448, 
-2.99847245406934, -3.57236610167573, -3.56963065732644, -2.8243000106372, 
-1.99565301030409, -3.3461888162997, -2.58940007000144, -3.19945356820737, 
-5.37830757225912, -2.73135885451205, -1.88970245530541, -2.54034752481066, 
-3.38038340627931, -3.13416288370415, -2.8610910675591, -2.89723228973215, 
-2.3992604730445, -2.68507391337318, -2.92441273949878, -3.33097198455173, 
-4.43334081889723, -4.53411741435393, -2.96651491236555), ID = c(1, 
 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 
52, 53, 54, 55, 56, 57, 58, 59, 60, 61), av = c(NA, NA, -5.15251619732328, 
-5.16265616988918, -4.93414170098446, -5.21461381245202, -5.21728637965377, 
-5.20512848276483, -4.75202655250037, -4.49175454648982, -4.1908889254143, 
-4.10666231694902, -4.00110723029239, -3.88289031355983, -3.77876992345444, 
 -3.51380616751619, -3.64112000988222, -3.44128250241695, -3.3815937910059, 
-3.45450238720312, -3.9099701595748, -3.96819996382322, -3.99366133905644, 
-4.16793050985004, -3.88987089011159, -3.61968642903037, -3.5603722380885, 
-3.63594840908544, -3.32245223224107, -3.15251868391762, -3.23541823364704, 
-2.99536019845129, -2.9675804850602, -3.27953363771187, -3.48158347425435, 
-3.0525576245942, -3.18153758428884, -3.16998575371184, -2.96027572957956, 
-2.96779670550464, -2.99208444680256, -3.06162771924863, -2.86503451291377, 
-2.79099909508996, -3.30180060741434, -3.44894177625593, -3.15764450405708, 
-3.14783399501892, -3.18401996263331, -2.73519102492232, -2.76113746753173, 
-2.96264343441708, -2.93442602406384, -2.79536412548262, -2.75341409664154, 
-2.84739028004007, -3.15461198587309, -3.58158337413497, -3.63787157393344, 
 NA, NA), yr = structure(c(-7305, -6940, -6575, -6209, -5844, 
-5479, -5114, -4748, -4383, -4018, -3653, -3287, -2922, -2557, 
-2192, -1826, -1461, -1096, -731, -365, 0, 365, 730, 1096, 1461, 
1826, 2191, 2557, 2922, 3287, 3652, 4018, 4383, 4748, 5113, 5479, 
5844, 6209, 6574, 6940, 7305, 7670, 8035, 8401, 8766, 9131, 9496, 
 9862, 10227, 10592, 10957, 11323, 11688, 12053, 12418, 12784, 
13149, 13514, 13879, 14245, 14610), class = "Date"), year = c("1950", 
"1951", "1952", "1953", "1954", "1955", "1956", "1957", "1958", 
"1959", "1960", "1961", "1962", "1963", "1964", "1965", "1966", 
"1967", "1968", "1969", "1970", "1971", "1972", "1973", "1974", 
"1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", 
"1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", 
"1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", 
 "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", 
"2007", "2008", "2009", "2010")), .Names = c("Mean_Tmin", "ID", 
 "av", "yr", "year"), row.names = c(NA, -61L), class = "data.frame").

I used this code to make a trend analysis plot with median based trend and intercept. my code so far is

   tplot<- ggplot(dt.anm, aes(yr,Mean_Tmin)) +
              geom_line(aes(y=Mean_Tmin),lty="dotted",color="Blue")+
              geom_point(aes(y=Mean_Tmin,colour="Mean_Tmin"),shape=8,size=5)+
              geom_line(aes(y=av,colour="5 yrs moving average",fill=F), size=0.8)+
              stat_smooth(method=lm)+
              geom_abline(aes(slope=0.0322,intercept=-4.581,color="red"))
    tplot

The problem is the abline is vertical and i do not know why it is not considering slope or intercept. Can anyone suggest me how can i change my code so that there will be horizontal abline with that slope and intercept.I am looking for something,like this but with year as x-axis. thank you, aseem

like image 311
Cirrus Avatar asked Feb 21 '14 23:02

Cirrus


1 Answers

Is this what you had in mind?

library(ggplot2)
slope <- 0.0322*4/(4*365+1)
int   <- -4.581 -slope*as.integer(df[1,]$yr)
tplot<- ggplot(df, aes(yr,Mean_Tmin)) +
  geom_line(aes(y=Mean_Tmin),lty="dotted",color="Blue")+
  geom_point(aes(y=Mean_Tmin,colour="Mean_Tmin"),shape=8,size=5)+
  geom_line(aes(y=av,colour="5 yrs moving average",fill=F), size=0.8)+
  stat_smooth(method=lm)+
  geom_abline(aes(slope=slope,intercept=int,color="red"))
tplot

As @joran suggests, ggplot is handling the x variable as "days since the unix epoch" (1970-01-01). You can see that by typing as.integer(df$yr). The 0 value will occur at 1970-01-01. The important thing is that the units of x are days, not years. So we have to scale the slope to account for that. This is a bit involved since we need to account for leap years.

To get the intercept, note that:

y - y0 = slope × (x - x0), or

y = y0 + slope × (x - x0)

Here, y0 = -4.581 and x0 is the integer value of 1950-01-01.

like image 64
jlhoward Avatar answered Nov 15 '22 09:11

jlhoward