

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
JeanPhilippe Fontaine

JeanPhilippe Fontaine
PhD Student in Astroparticle Physics,
Gran Sasso Science Institute (GSSI),
Viale Francesco Crispi 7,
67100 L'Aquila, Italy
Mobile: +393487128593, +33615653774
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Hi,
It's because you are plotting a line between each of the points in
your data frame, and they are very close together:
> cbind(df1$B,predict(regressor,df1))
[,1] [,2]
1 1.410832 13.96466
2 1.589383 15.21169
3 1.446662 14.21491
4 1.488665 14.50826
5 1.487035 14.49687
6 1.497347 14.56890
7 1.458070 14.29458
8 1.568134 15.06328
9 1.543364 14.89029
10 1.513473 14.68152
11 1.465462 14.34621
12 1.506752 14.63458
13 1.434703 14.13139
14 1.584011 15.17417
15 1.585621 15.18542
16 1.542410 14.88362
17 1.430091 14.09917
18 1.474529 14.40953
19 1.431341 14.10790
20 1.425015 14.06372
If instead you use abline(regressor, lty=2) you will get a
nicelooking dashed line.
Or, if you want only the data extent, you could use just the points
for the minimum and maximum values of x.
Sarah
On Mon, Dec 4, 2017 at 12:13 PM, jeanphilippe
< [hidden email]> wrote:
> 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
>
>
> JeanPhilippe Fontaine
>

Sarah Goslee
http://www.functionaldiversity.org______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


hi Sarah,
Thanks a lot for having taken time to answer me and for your reply. I
wonder how I missed this solution. Indeed plotting the line with the 2
extreme data points works perfectly.
Best,
JeanPhilippe Fontaine
On 04/12/2017 18:30, Sarah Goslee wrote:
> It's because you are plotting a line between each of the points in
> your data frame, and they are very close togethe

JeanPhilippe Fontaine
PhD Student in Astroparticle Physics,
Gran Sasso Science Institute (GSSI),
Viale Francesco Crispi 7,
67100 L'Aquila, Italy
Mobile: +393487128593, +33615653774
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Hi,
Sarah's last comment about using min/max x got me thinking. It's not that
the points are "very close together", it's that the xvalues are not
ordered. So the plot is actually drawing a dashed line backandforth
between different points on the line, which has the effect of making the
result appear nondashed. If you sort by the xvalues before plotting you
will see the output is very different. I am not saying this is a better
solution than Sarah's regarding just using the endpoints, but at least it
partially explains the relevance of that suggestion.
For example, here is a slightly modified version of the code that does an
ordering before plotting:
df1<data.frame(B=runif(20,1.4,1.6),A=runif(20,19.5,9.8))
regressor<lm(A~B,data = df1)
df2 < data.frame( x=df1$B, yhat= predict(regressor,df1))
df2 < df2[ order(df2$x), ]
plot(df2$x,df2$yhat,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)
HTH,
Eric
On Mon, Dec 4, 2017 at 8:30 PM, jeanphilippe <
[hidden email]> wrote:
> hi Sarah,
>
> Thanks a lot for having taken time to answer me and for your reply. I
> wonder how I missed this solution. Indeed plotting the line with the 2
> extreme data points works perfectly.
>
>
> Best,
>
>
> JeanPhilippe Fontaine
>
>
>
> On 04/12/2017 18:30, Sarah Goslee wrote:
>
>> It's because you are plotting a line between each of the points in
>> your data frame, and they are very close togethe
>>
>
> 
> JeanPhilippe Fontaine
> PhD Student in Astroparticle Physics,
> Gran Sasso Science Institute (GSSI),
> Viale Francesco Crispi 7,
> 67100 L'Aquila, Italy
> Mobile: +393487128593, +33615653774
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/posti> ngguide.html
> and provide commented, minimal, selfcontained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


In reply to this post by jeanphilippe fontaine
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: Rhelp [mailto: [hidden email]] On Behalf Of
jeanphilippe
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
JeanPhilippe Fontaine

JeanPhilippe Fontaine
PhD Student in Astroparticle Physics,
Gran Sasso Science Institute (GSSI),
Viale Francesco Crispi 7,
67100 L'Aquila, Italy
Mobile: +393487128593, +33615653774
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

