I have a plot where I am trying to compare the linear regression lines for various groups of interest to the linear fit for the sample as a whole.
library(ggplot2);library(curl)
df<-read.csv(curl("https://raw.githubusercontent.com/megaraptor1/mydata/main/example.csv"))
df$group<-as.factor(df$group)
ggplot(df %>% group_by(group),
aes(x,y,col=group))+
geom_point(size=2.5,shape=21,aes(fill=group),col="black")+
scale_x_continuous(breaks=seq(1,3.25,0.25))+
scale_y_continuous(breaks=seq(0,17,1))+
geom_smooth(data=df %>% group_by(group),
formula=y~x,method="lm",level=0.9,se=F)+
geom_smooth(data=df,linetype="dashed",fill="white",colour="black",size=.75)+
geom_smooth(data=df,linetype="dashed",fill="white",colour="black",size=1)+
labs(color="group",fill="group")+
guides(color=guide_legend(ncol=2),fill=guide_legend(ncol=2))+
theme_classic()+
theme(legend.position = "none")
In this graph the fit for all specimens is represented by a black dashed line. However this line doesn't stand out very much from the surrounding data and is hard to see. I would like to put a white outline surrounding the dashed black line in order to make it pop and stand out from the surrounding data.
However, I have been unable to do so via ggplot2. I know it is possible to do so via a non-dashed line by plotting a slightly thicker white line behind the black line. But if I try to do so with a dashed line it does not produce the desired affect (see above code), because changing the thickness of the line also changes the length and spacing of the dashes, and hence the two do not line up and make the black dashed line pop out.
Is there any way to set the border of a dashed line in ggplot to plot a white border around a black dashed line?



