0

Edit: found a solution to my problem! See answer below.

I have the following code that I'm trying to compile in R using the easyGgplot2 package:

ggplot2.scatterplot(data, xName='X', yName='Y', addRegLine=TRUE, groupName='DifferentClasses') +
  geom_point(aes(shape=DifferentClasses, color=DifferentClasses))+
  scale_shape_manual(values=c(7, 10, 19))+ 
  scale_color_manual(values=c('red','red','red'))

The scatterplot has the following characteristics (which I am being able to replicate correctly):

  1. 3 regression lines for different classes
  2. 3 different shapes for points
  3. Same color for all regression lines

What I am not being able to do is to change regression lines of classes "2" and "3" to different linetypes, say "dotdash" and "dotted".

I've already tried a bunch of different combinations for funcion linetype() but I wasn't able to get it working.

Anybody could help?

jkbgla
  • 1
  • 1
  • Where does the `ggplot2.scatterplot` function come from? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions – MrFlick Apr 16 '18 at 17:37
  • You're right! I forgot to which language I am trying to get this done, which is R, and the package I'm using (library(ggplot2)). Thanks for pointing out, and already edited the original post. – jkbgla Apr 16 '18 at 17:42
  • There is no `ggplot2.scatterplot` function in the ggplot2 package. Do you mean `easyGgplot2`? Those are not the same. – MrFlick Apr 16 '18 at 17:43
  • Yes, it's easyGgplot2, sorry! – jkbgla Apr 16 '18 at 17:44

1 Answers1

0

I've done some research and I actually found a solution to my problem and the full code follows bellow.

I'll explain which steps I took to format it in the way I wanted:

  1. used linetype = 'Class' inside ggplot2.scatterplot() to separate the lines by my class

  2. used geom_smooth(method="lm", se=FALSE, aes(linetype=Class)) to format the regression lines (only these lines) by different linetypes

  3. used scale_linetype_manual(values=c("solid", "longdash", "dotted")) to manually format the lines to the type I needed

The code follows bellow:

ggplot2.scatterplot(dataset, xName='XScale', yName='YScale', addRegLine=TRUE, groupName='Class', linetype = 'Class') +
    geom_smooth(method="lm", se=FALSE, aes(linetype=Class))+
    geom_point(aes(shape=Class, color=Class))+
    scale_linetype_manual(values=c("solid", "longdash", "dotted"))+
    scale_shape_manual(values=c(21, 22, 23))+ scale_color_manual(values=c('deepskyblue1','deepskyblue1','deepskyblue1'))+
    theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22
jkbgla
  • 1
  • 1