library(car)
## Loading required package: carData
library(carData)
library(ggplot2)

color codes

getwd()
## [1] "/Users/maj.mobajer/Desktop/R/Rmd "

Simple Scatter plot

ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point(size=2,color="#CC99CC")+
  #Add the regression line
  geom_smooth(method=lm,color="gray",fill="#6666CC",alpha=0.15)+
  labs(title = "Simple Scatter plot",
        x="Car weight",y="Miles per gallon")+theme_bw()
## `geom_smooth()` using formula 'y ~ x'

Scatter plot with categorical third variable

mtcars$cyl <- as.factor(mtcars$cyl)
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) +
  geom_point() + 
  #Add regression lines
  geom_smooth(method=lm, aes(fill=cyl),alpha=0.15)+
  labs(title = "Scatter plot with categorical variable") +theme_bw()
## `geom_smooth()` using formula 'y ~ x'

#change colors
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) +
  geom_point() + 
  #Add regression lines
  geom_smooth(method=lm, aes(fill=cyl),alpha=0.15)+
  scale_color_manual(values = c("#33CCCC", "#FF99CC", "#9933FF"))+
  scale_fill_manual(values =c("#33CCCC", "#FF99CC", "#9933FF"))+
  labs(title = "Scatter plot with categorical variable")+theme_bw()
## `geom_smooth()` using formula 'y ~ x'

#Remove the confidence interval
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) +
  geom_point() + 
  geom_smooth(method=lm, se=FALSE,aes(fill=cyl),alpha=0.15)+
  scale_color_manual(values = c("#33CCCC", "#FF99CC", "#9933FF"))+
  scale_fill_manual(values =c("#33CCCC", "#FF99CC", "#9933FF"))+
  labs(title = "Scatter plot with categorical variable") +theme_bw()
## `geom_smooth()` using formula 'y ~ x'

The different point shapes

#Extend the regression lines
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
  geom_point() + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
  labs(title = "Scatter plot with categorical variable") +theme_bw()
## `geom_smooth()` using formula 'y ~ x'

#change colors
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
  geom_point() + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
  scale_shape_manual(values=c(25, 16, 17))+ 
  scale_color_manual(values=c("#33CCCC","#CC99CC","#9933FF"))+
  labs(title = "Scatter plot with categorical variable")+theme_bw()
## `geom_smooth()` using formula 'y ~ x'

Scatter plot for each group

#Using facet_wrap() 
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) +
  geom_point() + facet_wrap(~cyl)+
  geom_smooth(method=lm, aes(fill=cyl),alpha=0.15)+
  labs(title = "Scatter plot for each group")+theme_bw()
## `geom_smooth()` using formula 'y ~ x'

##change colors
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) +
  geom_point() +facet_wrap(~cyl)+
  geom_smooth(method=lm, aes(fill=cyl),alpha=0.15)+
  scale_color_manual(values = c("#33CCCC", "#FF99CC", "#9933FF"))+
  scale_fill_manual(values =c("#33CCCC", "#FF99CC", "#9933FF"))+
   labs(title = "Scatter plot for each group")+theme_bw()
## `geom_smooth()` using formula 'y ~ x'

Scatter plot With Numeric third variable

ggplot(mtcars, aes(x=wt, y=mpg, color=disp))+
   geom_point(aes(color = disp), size = 2) +
   scale_color_gradientn(colors = c("#33CCCC","#FF99CC", "#9933FF")) +
   labs(title = "Scatter plot with Numeric third variable")+theme_bw()

The end