How to add a regression line and change colors in ggplot smoothscatter?
1
0
Entering edit mode
5.5 years ago
Zee_S ▴ 60

I would like to know a script template to do the above. can someone please help me?

ggplot smoothscatter R • 5.1k views
ADD COMMENT
0
Entering edit mode

Please show us you've invested some effort on your own trying to solve this. You seem to know the terms/jargon, have you tried googling? How far did you get?

ADD REPLY
0
Entering edit mode

hello!

thank you so much for your comments. Im sorry for not being more elaborate.

geom_smooth() doesn't work for me with smoothScatter function. perhaps I'm not adding it at the right layer?

I would also like to change the bandwidth for smoothing, to get less blurred data points at the peripheries. Can you please tell me how to do this? At the moment, with default bandwidth, my plot is oversmoothed.

I post here a template of the code i wrote for my plot:

myplot <- read.table(file = "./file.tsv",sep = "\t", dec = ".",header = TRUE)
head(myplot)
smoothScatter(myplot$x, myplot$y, nrpoints=0,
                        xlim=c(-3,3), ylim=c(-3,3),
                        xlab = "X", ylab = "Y", 
                        cex.lab=1.5, 
                        cex.axis=1.5)

Thank you so much for your help!

ADD REPLY
0
Entering edit mode

Please use Add comment or put this comment in your question instead of using the answer field for this. Also, please reformat this comment by marking code with the code buttom in the formating bar (10101). By the way, doesnt work for me, is most uninformative. Please elaborate.

ADD REPLY
0
Entering edit mode

Thanks ATpoint. Zee_S, why use smoothScatter? - it uses base graphics. The solution that both Nicolas and I mentioned requires ggplot2.

To put a linear regression fit on a smoothScatter plot, you can just do this:

x <- matrix(rexp(200, rate=.1), ncol=20)
smoothScatter(x)
abline(lm(x[,1] ~ x[,2]), col="red2", lty=1, lwd=3)

h

--------------------------------------------------

You may also be interested in 'hexagonal binning': A: Scatterplot with hexbin

c

ADD REPLY
1
Entering edit mode
5.5 years ago

geom_smooth() should suit your demand :

https://ggplot2.tidyverse.org/reference/geom_smooth.html

for the color : scale_color_manual() can be a start

But you should provide us with more information. What did you already try ?

ADD COMMENT
0
Entering edit mode

Indeed, for a linear regression model, you can plot real versus the model-predicted values and plot the linear fit line via geom_smooth()

k

If you want to know how to do all of this, then take a look at RTrainingLect3.pptx, here: https://github.com/kevinblighe/Rtutorials

ADD REPLY

Login before adding your answer.

Traffic: 2672 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6