如何在xg轴作为年份的ggplot2中添加abline?

时间:2014-02-21 23:00:56

标签: r date plot ggplot2

我正在尝试使用ggplot2制作温度趋势图。 我不能用给定的斜率和截距绘制abline(它们是Sen Estimate用于趋势计算的结果)。

df是我数据的子集。

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").

我使用此代码制作基于中位数趋势和截距的趋势分析图。到目前为止,我的代码是

   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

问题是abline是垂直的,我不知道为什么它不考虑斜率或截距。 任何人都可以建议我如何更改我的代码,以便有斜率和拦截的水平abline。我正在寻找一些东西,如this但年份为x轴。 谢谢, aseem

1 个答案:

答案 0 :(得分:8)

这是你的想法吗?

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

正如@joran所暗示的那样,ggplot将x变量视为“自unix时代以来的天数”(1970-01-01)。您可以通过输入as.integer(df$yr)来查看。 0值将出现在1970-01-01。重要的是x的单位是天,而不是年。所以我们必须缩放斜率来解释这个问题。这有点牵扯,因为我们需要考虑闰年。

要获得拦截,请注意:

  

y - y 0 =斜率×(x - x 0 )或

     

y = y 0 +斜率×(x - x 0

此处,y 0 = -4.581且x 0 是1950-01-01的整数值。