[R] problem with the behaviour of dashed lines in R plots

Duncan Mackay dulcalma at bigpond.com
Mon Dec 4 23:41:17 CET 2017


If you can change the line colour try this

options(mgp=c(2,0.5,0),cex.lab=1.6)

plot(df1$B, predict(regressor,df1),
     type="l",
     col="grey",
     lwd=2,
     lty=1,
     xlim=range(c(1.2,1.7)),
     ylim=rev(range(c(-19,-8))))
     
lines(df1$B,as.numeric(df1$A),
      type="p",
      col="black",
      cex=2,
      pch=17)

box(lwd=3)

Saves typing by using options

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2350


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of
jean-philippe
Sent: Tuesday, 5 December 2017 04:14
To: R mailing list
Subject: [R] problem with the behaviour of dashed lines in R plots

dear R users,

I am performing a linear regression with lm, and I would like to plot 
the regressor in dashed lines. I know that the lty=2 option is the way 
out, but it has a very strange behaviour: the line starts dashed but 
then the spaces between each dash becomes very tiny and so the line 
become somehow continuous for the human eye. Do you know how to fix that 
problem, in order to have a dashed line with big enough spaces between 
the dashes?
Here is a MWE (don't mind if clearly a linear model will not fit these 
"fake randomly generated" data).

Also, changing the plot(...,type="l") to abline(regressor,lty=2,...) 
helps and draw a pure dashed line but it is impossible to force it to 
stay in the bounds of the data.
Changing to line instead shows the same problem as mentioned here in the 
MWE.


pdf("reproducableex.pdf")
df1<-data.frame(B=runif(20,1.4,1.6),A=runif(20,-19.5,-9.8))
regressor<-lm(A~B,data = df1)
plot(df1$B,predict(regressor,df1),type="l", col="black", 
mgp=c(2,0.5,0),cex.lab=1.6, lwd=2, 
lty=2,xlim=range(c(1.2,1.7)),ylim=rev(range(c(-19,-8))))
par(new = TRUE)
plot(df1$B,as.numeric(df1$A),type="p", col="black", 
mgp=c(2,0.5,0),cex.lab=1.6,cex=2, xlab = "", ylab = 
"",xlim=range(c(1.2,1.7)),ylim=rev(range(c(-19,-8))),pch=17)
box(lwd=3)
dev.off()


Thanks in advance, best regards


Jean-Philippe Fontaine

-- 
Jean-Philippe Fontaine
PhD Student in Astroparticle Physics,
Gran Sasso Science Institute (GSSI),
Viale Francesco Crispi 7,
67100 L'Aquila, Italy
Mobile: +393487128593, +33615653774

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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