2

Hello I have a smooth scatter plot same plot I wanted try with ggplot with, can anyone help me i have created plot using ggplot but not able create curve line and diagonal line same as smooth scatter plot

data

   A    B   cat
0.8803  0.0342  data1
0.9174  0.0331  data1
0.9083  0.05    data1
0.7542  0.161   data2
0.8983  0.0593  data2
0.8182  0.1074  data2
0.3525  0.3525  data3
0.5339  0.2288  data3
0.7295  0.082   data3

smooth scatter plot

df=read.table("test.txt", sep='\t', header=TRUE)
smoothScatter(df$B,df$A,,nrpoints=Inf,xlim=c(0,1),ylim=c(0,1), pch=20,cex=1, col=df$cat)
points(c(0,1),c(1,0),type='l',col='green',lty=2,lwd=2)
p=0:1000/1000
points((1-p)^2,p^2,type='l',col='red',lty=2,lwd=2)

enter image description here

ggplot script

ggplot(df, aes(x=B, y=A))+
  geom_point()
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Shrilaxmi M S
  • 151
  • 1
  • 12
  • Please provide reproducible code for your data (the df object). You could do so by pasting the output of dput(df). That said, you should see that, for every image 'object' (geoms such as lines, points), ou should add a geom, with `geom_smooth()` or `geom_line()`, and many others. For how to make a smoothScatter in ggplot, there may be answers here : https://stackoverflow.com/questions/13094827/how-to-reproduce-smoothscatters-outlier-plotting-in-ggplot – GuedesBF Apr 10 '21 at 14:26

1 Answers1

0

If you're trying to draw lines based on an equation, you can define the equation and then use geom_line to draw that line with stat="function". Here's how you can draw the lines in ggplot and simulate the same look:

library(ggplot2)

curvy <- function(x) { ((1-x)^2)^2 }
straight <- function(x) 1-x

ggplot(df, aes(x=B, y=A))+
  geom_point(size=3) +
  geom_line(stat='function', fun=straight, color='green', linetype='dashed', size=1) +
  geom_line(stat='function', fun=curvy, color='red', linetype='dashed', size=1) +
  xlim(0,1) + ylim(0,1) +
  theme_classic()

enter image description here

As for the fuzzy points, you can give ggblur a try here's the github. It wasn't available for the version I'm using.

The other way to draw lines on a plot via regression would be to use geom_smooth(). You'll want to specify the method there - for linear you can use "lm" - "loess" is used by default.

ggplot(df, aes(x=B, y=A))+
  geom_point(size=3) +
  geom_line(stat='function', fun=straight, color='green', linetype='dashed', size=1) +
  geom_line(stat='function', fun=curvy, color='red', linetype='dashed', size=1) +
  geom_smooth(method='lm', alpha=0.2, color='blue', fill='skyblue', linetype='dotted') +
  xlim(0,1) + ylim(0,1) +
  theme_classic()

enter image description here

chemdork123
  • 12,369
  • 2
  • 16
  • 32