[R] Plotting does odd line thing

Frede Aakmann Tøgersen frtog at vestas.com
Tue Apr 8 07:52:57 CEST 2014


Well the lines() function has a subset argument as does plot() so why not do this?

lines(EMD ~ Year, data = mydata, subset = Well.ID %in% c("MW-1", "D_EMD"))

And yes lines() also have a data argument so why do you not use that (as you do with plot()) instead of attaching.

Attaching dataframes may be a potential danger when some time in the future you have forgot that you attached the dataframe and you have forgot the warnings you get when attaching (making debugging difficult).

Try this:

> ## Define an object named Year
> ## in which case call to lines do not what you think it will do
> Year <- 1:nrow(mydata) 
>
> ## or Year <- 'foo' in which case one gets an error later
>
> ## attach the dataframe
>  attach(mydata)

The following object is masked _by_ .GlobalEnv:

    Year

Now what will happen if you do lines(EMD ~ Year)?????  

Here is the search path in my session:

> search()
 [1] ".GlobalEnv"        "mydata"            "package:lattice"  
 [4] "package:RODBC"     "package:stats"     "package:graphics" 
 [7] "package:grDevices" "ESSR"              "package:utils"    
[10] "package:datasets"  "package:methods"   "Autoloads"        
[13] "package:base"     


Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Jim Lemon
> Sent: 8. april 2014 04:53
> To: David Doyle
> Cc: r-help at r-project.org
> Subject: Re: [R] Plotting does odd line thing
> 
> On 04/08/2014 12:20 PM, David Doyle wrote:
> > Hello folks,
> >
> > When I use the lines function below it connects all my points but then
> > draws a line back to the start point.  Any suggestions on what is going on??
> >
> > mydata<-read.csv("http://doylesdartden.com/R/test_data.csv", sep=",")
> >
> > attach(mydata)
> >
> > plot(EMD~Year,data=mydata, subset = Well.ID %in% c("MW-1", "D_EMD"),
> > col=ifelse(D_EMD, "black", "red"), pch=ifelse(D_EDM, 19, 17), cex = 1.5)
> >
> > lines(EMD~Year)
> >
> Hi David,
> While you will get what you expect with:
> 
> lines(EMD[1:39]~Year[1:39])
> 
> I would be unnecessarily obscure in suggesting it. Try this:
> 
> subset<-Well.ID %in% c("MW-1", "D_EMD")
> lines(EMD[subset]~Year[subset])
> 
> You haven't selected the same points for the lines function as you have
> for the plot function.
> 
> Jim
> 
> ______________________________________________
> 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.




More information about the R-help mailing list