Hi,
It is because of the unequal lengths of x and y.
I was able to plot without the errors.? But, not sure if this is what you
wanted.
mydata <- read.table("TestData.csv", header=TRUE,
sep="\t")
reg1<-lm(MW01~Year,data=mydata)
?plot(MW01~Year,data=mydata[!is.na(mydata$MW01),],col=ifelse(D_MW01,"black","red"),ylab="END
(mg/L)",pch=ifelse(D_MW01,19,24),cex=1)
with(mydata[!is.na(mydata$MW01),],lines(Year,MW01,lty=c(1),col="black"))
plx1<-predict(loess(MW01 ~ Year, data=mydata[!is.na(mydata$MW01),]), se=T)
#rough & ready CI by adding and subtracting 2 times the standard error to
the mean
lines(mydata[!is.na(mydata$MW01),"Year"],plx1$fit+2*plx1$s, lty=2)
lines(mydata[!is.na(mydata$MW01),"Year"],plx1$fit-2*plx1$s, lty=2)
mydata1<-mydata[!is.na(mydata$MW01),2:3]
y.loess <- loess(y ~ x, span=0.8, data.frame(x=mydata1$Year,y=mydata1$MW01))
# Compute loess smoothed values for all points along the curve
y.predict <- predict(y.loess, data.frame(x=mydata1$Year))
# Plots the curve.
lines(mydata1$Year,y.predict, lty=2, lwd=2)
BTW,? "Non-detect" in the legend is not clear.
A.K.
#Add line between the points
lines(mydata1$Year,mydata1$MW01)
________________________________
From: David Doyle <kydaviddoyle at gmail.com>
To: arun <smartpink111 at yahoo.com>
Sent: Tuesday, May 14, 2013 11:10 PM
Subject: Re: [R] Broken line questions
Hi,
I'm trying to do something very?similar?to the graph before and I got the
broken line problem fixed with your example.
On this one I'm trying to add 2 times the standard error (approxmating a 10%
confidence) onto the LOESS curve.
The data is at
https://docs.google.com/file/d/0B0CqEkyKWiq9SjFwemRhZnV4aTA/edit?usp=sharing
(sorry I don't have access to my FTP right now.)
When I use the code below, I get?
"Error in xy.coords(x, y) : 'x' and 'y' lengths
differ"
I assume this is because there are blanks in the data. ?
Any?suggestions???
The code I'm ?using is:
#set the dir to where you data is
setwd("c:/R")
#Load your data. ?The data is in a spreadsheet named nd-spreadsheet and we are
going to call it data in R
mydata <- read.table("TestData.csv", header=TRUE,
sep=",",)
attach(mydata)
reg1 <- lm(MW01~Year)
par(cex=1)
#Plots the data but makes nondetects a different color and type based on column
D_MW01 being a 0 for ND and 1 for detect.
plot(MW01~Year,data=mydata[!is.na(mydata$MW01),], col=ifelse (D_MW01,
"black", "red"),ylab = "EMD (mg/L)",
pch=ifelse(D_MW01, 19, 24), cex = 1)
with(mydata[!is.na(mydata$MW01),],lines(Year,MW01,lty=c(1),col="black"))
plx<-predict(loess(MW01 ~ Year, data=mydata), se=T)
#rough & ready CI by adding and subtracting 2 times the standard error to
the mean
lines(mydata$Year,plx$fit+2*plx$s, lty=2)?
lines(mydata$Year,plx$fit-2*plx$s, lty=2)
# Apply loess smoothing using the default span value of 0.8. ?You can change the
curve by changing the span value.
y.loess <- loess(y ~ x, span=0.8, data.frame(x=Year, y=MW01))
# Compute loess smoothed values for all points along the curve
y.predict <- predict(y.loess, data.frame(x=Year))
# Plots the curve.
lines(Year,y.predict, lty=2, lwd=2)
#Add line between the points
lines(Year,MW01)
#Add Legend to MW01. ?You can change the size of the box by changing cex = 0.75
?Large # makes it larger.
legend ("topleft",?
c("Smoothing Curve","Detect","Non-Detect"),?
col = c(1,1,1),
cex = 1,?
text.col = "black",?
lty = c(2,-1,-1),?
lwd = c(2,-1,-1),
pch = c(-1,19,24),?
merge = TRUE, bg = 'gray90')
#Add title
title(main="MW01")
# Done
Thanks again
David
On Sun, May 12, 2013 at 10:11 AM, arun <smartpink111 at yahoo.com> wrote:
No problem,>Regards,
>Arun
>
>
>
>
>
>
>
>________________________________
>From: David Doyle <kydaviddoyle at gmail.com>
>To: arun <smartpink111 at yahoo.com>
>Sent: Sunday, May 12, 2013 8:03 AM
>Subject: Re: [R] Broken line questions
>
>
>
>
>Thanks!
>
>That took care of it.
>
>
>
>On Sat, May 11, 2013 at 10:01 PM, arun <smartpink111 at yahoo.com>
wrote:
>
>Hi,
>>May be this helps:
>>plot(dataset1~Date,data=mydata[!is.na(mydata$dataset1),],ylim=range(5.7,8),pch=10,cex=0.8,col="black",xlab="Date",ylab="pH")
>>?with(mydata[!is.na(mydata$dataset2),],points(Date,dataset2,col="blue",pch=2,cex=0.8))
>>?with(mydata[!is.na(mydata$dataset3),],points(Date,dataset3,col="red",pch=2,cex=0.8))
>>?with(mydata[!is.na(mydata$dataset1),],lines(Date,dataset1,lty=c(1),col="black"))
>>?with(mydata[!is.na(mydata$dataset2),],lines(Date,dataset2,lty=c(1),col="blue"))
>>?with(mydata[!is.na(mydata$dataset3),],lines(Date,dataset3,lty=c(1),col="red"))
>>
>>
>>
>>A.K.
>>
>>
>>
>>
>>----- Original Message -----
>>From: David Doyle <kydaviddoyle at gmail.com>
>>To: r-help at r-project.org
>>Cc:
>>Sent: Saturday, May 11, 2013 10:22 PM
>>Subject: [R] Broken line questions
>>
>>Hello Everyone,
>>
>>I have some data that like most real world data isn't complete.?
I'm trying
>>to plot all of it together with lines connecting the data points.?
Because
>>I have breaks in the data I have breaks in the lines.
>>
>>Is there a way that the lines will connect all the markers???? ?Below is
>>the code.
>>
>>Thanks in advance.
>>David
>>
>>#Load your data.? The data is in a spreadsheet named KW-spreadsheet and
we
>>are going to call it "data" in R
>>mydata <-
read.csv("http://doylesdartden.com/example-smoothing-data.csv";;,
>>sep=",")
>>attach(mydata)
>>
>>#Plots the Y and X axis
>>plot( Date, dataset1,
>>
>>#sets the range of the y axis
>>ylim=range(5.7,8),
>>
>>#sets the symbol type, size, and color for the 1st series
>>pch=10,cex=0.8, col='black', xlab="Date ",
ylab="pH")
>>
>>#Plots the second series
>>points( Date, dataset2, col='blue',pch=2, cex=0.8)
>>
>>#Plots the 3rd series
>>points( Date, dataset3, col='red',pch=3, cex=0.8)
>>
>>#Lines
>>lines(Date, dataset1, lty = c(1),col='black')
>>lines(Date, dataset2, lty = c(2),col='blue')
>>lines(Date, dataset3, lty = c(3),col='red')
>>
>>#Add Legend to graph.? You can change the size of the box by changing
cex >>0.75? Large # makes it larger.
>>legend("bottomleft",c("Dataset1","Dataset2",
"Dataset3"),
>>col = c("black","blue", "red"),
>>cex = 0.7,text.col = "black",lty = c(1,2,3),lwd=c(2),pch =
c(10,2,3),
>>merge = TRUE, bg = 'gray90')
>>
>>#Add title
>>title(main="Time Series Plot")
>>#done
>>
>>??? [[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help at r-project.org mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.
>>
>>
>