I'd like to analyse monthly rainfall data (make time series plot + regression equation for time series). I've written code in R and plot the monthly time series data and I've tried to make different regression equations (linear and non-linear) and show these equation on the same graph of time series plot but unfortunately I cannot. May be because I'm new user of R / Rstudio statistical packages.
Data style
Date monthly rainfall (mm)
jan94 12
Feb94 11
.
.
. Dec14 1x
The code
# plotting of time series rainfall data (option1)
# step1: read files
MR<-read.table("C:\\Users\\Salam\\Desktop\\trend Kufa\\CSV2 Habel\\Monthly rainfall.csv", header=T,sep=",")
summary(MR)
names(MR)
MR
# step2: plot observed discharge
MR1<-MR[c(1:252),2];
summary (MR1)
MR1
class(MR1)
require(zoo)
x <- yearmon(1994 + seq(0, 251)/12)
x
y<-MR1
y
pcp<-y~x
plot(pcp,type="l", xlab="Month",ylab="Monthly Rainfall(mm)", axes=T)
grid(nx=250, ny=250, col="lightgray", lty="solid")
lines(pcp,lwd=2, col="blue")
box(which='plot')
title("Monthly Observed rainfall(mm)")
## Regression
S1 <- lm(y ~ z, data=MR)
abline(S1,col='red',lwd=3)
summary(S1)
S2<-lm( y~poly(x,3), data=MR)
summary(S2)
abline(S2,col='green',lwd=3)
S3 <- nls(y ~ exp(a + b / x),start = list(a = 0, b = 0))
summary(S3)
S4 <- nls(y ~ (a + b *log( x)), start = list(a = 0, b = 0))
summary(S4)
You can use the text function to put the equations on the plots.
text(x, y, "S1 <- lm(y ~ z, data=x)",
cex = .8)
the x and y are the coordinates on the plot where you would like the equation
put the equation in quotes
data is your data frame
cex controls the font size
for more info & options on text use ?text
Check the following example and you can modify yours easily.
library(ggplot2)
# example dataset
dt = data.frame(date = 1:10,
value = c(10,11,15,13,16,17,18,19,16,22))
# plot everything in one graph
ggplot(dt, aes(date, value)) +
geom_point() + # plot the points
stat_smooth(method="lm",se=F,level=0.95, col="red") + # linear reg
stat_smooth(method="lm", formula = y~poly(x,2,raw=T), se=F,level=0.95, col="blue") + # quadratic reg
stat_smooth(method="lm", formula = y~poly(x,3,raw=T), se=F,level=0.95, col="green") # cubic reg
# plot everything in separately
library(gridExtra)
plot1 = ggplot(dt, aes(date, value)) +
geom_point() +
stat_smooth(method="lm",se=T,level=0.95, col="red")
plot2 = ggplot(dt, aes(date, value)) +
geom_point() +
stat_smooth(method="lm", formula = y~poly(x,2,raw=T), se=T,level=0.95, col="blue")
plot3 = ggplot(dt, aes(date, value)) +
geom_point() +
stat_smooth(method="lm", formula = y~poly(x,3,raw=T), se=T,level=0.95, col="green")
grid.arrange(plot1,plot2,plot3)
I hope you are familiar with the ggplot2 package as it is the most important in this case. You can then investigate ways to add titles, change colours, change confidence intervals, etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.