0

I am creating a scatter plot between a dependent variable FA and subject age (in yrs) that has 2 group conditions (Exercise and Rest).

I am having issues setting the regression lines so that I can have the regression line for the Exercise group be solid and the Rest group be dotted.

It seems as if ggscatter won't allow me to provide 2 different linetypes in add.params = list(linetype=c("solid", "dotted"))?

I have looked at the source code and using

add.params = list(linetype="dotted") changes the linetype for both Ex and Rest group. But when trying add.params = list(linetype=c("solid", "dotted")) I get the error

Error: Aesthetics must be either length 1 or the same as the data (160): linetype

diffusion_data <-
structure(list(FA_full_cov = c(0.153232, 0.164497, 0.111886, 
0.14139, 0.130546, 0.18607, 0.181865, 0.139148, 0.178903, 0.136147, 
0.140427, 0.143346, 0.140975, 0.148248, 0.128336, 0.147552, 0.126607, 
0.127531, 0.153574, 0.124305, 0.168183, 0.146543, 0.135313, 0.139777, 
0.148862, 0.154091, 0.131398, 0.145124, 0.136015, 0.128609, 0.159028, 
0.158221, 0.124092, 0.139492, 0.142623, 0.195182, 0.229651, 0.144567, 
0.169234, 0.181687, 0.136057, 0.14369, 0.143988, 0.152487, 0.109607, 
0.139264, 0.139382, 0.13402, 0.159948, 0.141635, 0.177908, 0.133823, 
0.196866, 0.204928, 0.15321, 0.150005, 0.126811, 0.158618, 0.135901, 
0.147437), age = c(63, 57, 75, 75, 72, 58, 60, 63, 56, 58, 65, 
81, 65, 65, 77, 74, 74, 67, 55, 56, 79, 59, 64, 71, 60, 63, 70, 
68, 74, 68, 63, 57, 75, 75, 72, 58, 60, 63, 56, 58, 65, 81, 65, 
65, 77, 74, 74, 67, 55, 56, 79, 59, 64, 71, 60, 63, 70, 68, 74, 
68), Conditions = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L), .Label = c("Ex", "Rest"), class = "factor")), row.names = 
c(NA, 60L), class = "data.frame")

library(ggpubr)

a = ggscatter(diffusion_data, y="FA_full_cov", x="age", color = "Conditions", palette = c("black", "grey39"), shape = "Conditions", add = "reg.line", add.params = list(linetype=c("solid", "dotted")), conf.int = TRUE, cor.coef = TRUE, cor.method = "pearson", cor.coef.size = 5, cor.coef.coord = c(70,.25)) +
  ggtitle("Hippocampal FA with Respect to Age") + xlab("Age (years)") + ylab("FA") +  theme(plot.title = element_text(hjust = 0.5, size = 30)) +
  theme(axis.text.x = element_text(size = 20)) + 
  theme(axis.text.y = element_text(size = 15)) +
  theme(axis.title.y = element_text(size = 20)) + 
  theme(axis.title.x = element_text(size = 20)) +
  theme(legend.text = element_text(size=15)) + 
  scale_shape_manual(values = c(16,1)) 

ggpar(a, ylim = c(.05,.25))

a

FA_full_cov and age are continuous variables and Conditions has 2 factors (Ex and Rest)

Image of graph when just using add.params = list(linetype="dotted"):

enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94
Daniel Callow
  • 101
  • 1
  • 6
  • Welcome to SO. Please provide some data. We want to help, but answering takes time, which we donate. So make an effort please and create a [reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Otherwise it is very difficult to help you. Actually - no one would help you. – tjebo Jul 24 '19 at 15:06
  • I made some edits to how I got the data in the current format, but I am unable to attach or embed a picture of the graph or add my excel file. Is there another way to provide a more reproducible example? – Daniel Callow Jul 24 '19 at 17:00
  • Never mind I see how to include an image. Are you saying I need to include the excel file/data to provide a reproducible example? If so how would I go about doing this? – Daniel Callow Jul 24 '19 at 17:05
  • Have a look here - it explains several ways to put in some sample data https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – tjebo Jul 24 '19 at 17:10
  • FYI I have removed harmful code from your question - please don't put code in there which changes our working directory. Create a sample data frame instead, e.g. using `dput()` - have a look at the previous link – tjebo Jul 24 '19 at 17:15
  • Thank you for making the edits as well as providing me with the information to help provide my data set. I have included my data with the dput() function above. Thank you for your help thus far Tjebo! – Daniel Callow Jul 24 '19 at 18:12

3 Answers3

1

Below is the code I used to recreate the graph but using ggplot instead of ggscatter I was able to change the linetype and color by group with geom_smooth()

b = ggplot(diffusion_data, aes(age,FA_full_cov, shape=Conditions, color = 
Conditions)) + geom_point(size=2.5)+ scale_color_manual(values = c("black", 
"grey39"))+ 
geom_smooth(aes(linetype = Conditions, fill=Conditions), method = "lm", formula = 
y~x, color ="black") +
scale_fill_manual(values = c("black", "grey39")) +
scale_shape_manual(values = c(16,1)) +
ggtitle("Hippocampal FA with Respect to Age") + xlab("Age (years)") + ylab("FA") +  
theme_bw() +
theme(
plot.title = element_text(hjust = 0.5, size = 30),
axis.text.x = element_text(size = 20),
axis.text.y = element_text(size = 15),
axis.title.y = element_text(size = 20),
axis.title.x = element_text(size = 20),
legend.text = element_text(size=15),
legend.text.align = 0,
legend.position = "top"

)

Daniel Callow
  • 101
  • 1
  • 6
0

Hmm, I think what you want to achieve is pretty difficult with ggpubr::ggscatter(btw, you should ideally add the library() call). But it's very easy with ggplot2!

If you are open to using 'simple ggplot' and not some crazy package which is build on top of it, then here is a solution:


library(ggplot2)
ggplot(diffusion_data, aes(age, FA_full_cov, color = Conditions))+
  geom_point() + # draw the points
  geom_smooth(aes(linetype = Conditions)) + # draw the regression curve/ line. 
    # For regression lines, specify method = 'lm'
  scale_color_brewer(palette = 'Greys') # just for the sake of it
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Created on 2019-07-24 by the reprex package (v0.2.1)

You can easily change the linetype using scale_linetype_manual

Note that I have removed big parts of your plot code because most of it wasn't necessary for the actual problem.

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Thank you for your reply. I will look into doing it this way. – Daniel Callow Jul 24 '19 at 19:31
  • My vote does not show, however, with your help I was able to create the graph using ggplot instead. – Daniel Callow Jul 25 '19 at 16:08
  • That's fine, glad I could help. If you are happy with the answer, then you can accept it using the checkmark next to my answer. This way the question will not show up as "unanswered". If you're not quite happy, leave it unaccepted ! – tjebo Jul 26 '19 at 10:32
0

In ggscatter try:

add.params = list(linetype = "Conditions") 
zx8754
  • 52,746
  • 12
  • 114
  • 209
aes
  • 1
  • Maybe you could expand your explanation to have more complete code? That would make the answer more useful to the community. – xilliam Oct 04 '22 at 09:40