library(ggplot2)
data(mtcars)

# Load the data
df <- mtcars
df <- mtcars[, c("mpg", "cyl", "wt")]
# Convert cyl to a factor variable
df$cyl <- as.factor(df$cyl)

I. qplot(): Quick plot with ggplot2

A simplified format of qplot() is :qplot(x, y = NULL, data, geom=“auto”)

1. Basic scatter plot

qplot(x = mpg, y = wt, data = df, geom = "point")
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.

# Scatter plot with smoothed line
qplot(mpg, wt, data = df, geom = c("point", "smooth"))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

qplot(mpg, wt, data = df, colour = cyl, shape = cyl) # the color and the shape of points will be changed by the levels of cyl.

2. Box plot, violin plot and dot plot

set.seed(13032019)
wdata = data.frame(
  sex = factor(rep(c("F", "M"), each=200)),
  weight = c(rnorm(200, 55), rnorm(200, 58)))

# Basic box plot from data frame
qplot(sex, weight, data = wdata, geom= "boxplot", fill = sex)

# Violin plot
qplot(sex, weight, data = wdata, geom = "violin")

qplot(sex, weight, data = wdata, geom = "violin", fill = sex)

# Dot plot
qplot(sex, weight, data = wdata, geom = "dotplot",
      stackdir = "center", binaxis = "y", dotsize = 0.5)
## Bin width defaults to 1/30 of the range of the data. Pick better value with
## `binwidth`.

3. Histogram and density plots

The histogram and density plots are used to display the distribution of data.

# Histogram  plot
# Change histogram fill color by group (sex)
qplot(weight, data = wdata, geom = "histogram", fill = sex)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Density plot
# Change density plot line color by group (sex)
# change line type
qplot(weight, data = wdata, geom = "density", color = sex, linetype = sex)

II. ggplot(): build plots piece by piece

ggplot divides a plot into three different fundamental parts: plot = data + Aesthetics + geometry.

1. Basic scatter plot

ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point()

# Change the point size, and shape
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(size = 2, shape = 23)

# Use geometry function
ggplot(wdata, aes(x = weight)) + geom_density()

# OR use stat function
ggplot(wdata, aes(x = weight)) + stat_density()

library(plyr)
mu <- ddply(wdata, "sex", summarise, grp.mean=mean(weight))
head(mu)
##   sex grp.mean
## 1   F 55.02405
## 2   M 57.93254
a <- ggplot(wdata, aes(x = weight))

Possible layers are:

For one continuous variable: geom_area() for area plot geom_density() for density plot geom_dotplot() for dot plot geom_freqpoly() for frequency polygon geom_histogram() for histogram plot stat_ecdf() for empirical cumulative density function stat_qq() for quantile - quantile plot

For one discrete variable: geom_bar() for bar plot #############################################

geom_area(): Create an area plot

# Basic plot
  a + geom_area(stat = "bin")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

  # change fill colors by sex
  a + geom_area(aes(fill = sex), stat ="bin", alpha=0.9) + theme_classic()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

    a + geom_area(aes(y = ..density..), stat ="bin")
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

  a + stat_bin(geom = "area")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

geom_density(): Create a smooth density estima

geom_density() to create a density plot geom_vline() to add a vertical lines corresponding to group mean values scale_color_manual() to change the color manually by groups

# Basic plot
  a + geom_density()

  # change line colors by sex
  a + geom_density(aes(color = sex)) 

  # Change fill color by sex
  # Use semi-transparent fill: alpha = 0.4
  a + geom_density(aes(fill = sex), alpha=0.4)

  # Add mean line and Change color manually
  a + geom_density(aes(color = sex)) +
    geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
               linetype="dashed") +
    scale_color_manual(values=c("#999999", "#E69F00")) 

geom_dotplot(): Dot plot

In a dot plot, dots are stacked with each dot representing one observation.

# Basic plot
  a + geom_dotplot()
## Bin width defaults to 1/30 of the range of the data. Pick better value with
## `binwidth`.

  # change fill and color by sex
  a + geom_dotplot(aes(fill = sex)) 
## Bin width defaults to 1/30 of the range of the data. Pick better value with
## `binwidth`.

  # Change fill color manually 
  a + geom_dotplot(aes(fill = sex)) +
    scale_fill_manual(values=c("#999999", "#E69F00"))  
## Bin width defaults to 1/30 of the range of the data. Pick better value with
## `binwidth`.

geom_freqpoly(): Frequency polygon

# Basic plot
  a + geom_freqpoly() 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

  # change y axis to density value
  # and change theme
  a + geom_freqpoly(aes(y = ..density..)) +
    theme_minimal()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

  # change color and linetype by sex
  a + geom_freqpoly(aes(color = sex, linetype = sex)) +
    theme_minimal()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

geom_histogram(): Histogram

 # Basic plot
  a + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

  # change line colors by sex
  a + geom_histogram(aes(color = sex), fill = "white",
                     position = "dodge") 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

stat_ecdf(): Empirical Cumulative Density Function

a + stat_ecdf()

stat_qq(): quantile - quantile plot

ggplot(mtcars, aes(sample=mpg)) + stat_qq()  

One variable: Discrete

The function geom_bar() can be used to visualize one discrete variable. In this case, the count of each level is plotted. We’ll use the mpg data set [in ggplot2 package]. The R code is as follow:

data(mpg)
  b <- ggplot(mpg, aes(fl))
  # Basic plot
  b + geom_bar()

  # Change fill color
  b + geom_bar(fill = "steelblue", color ="steelblue") +
    theme_minimal()      

  b + stat_count()  

Source: http://www.sthda.com/english/wiki/be-awesome-in-ggplot2-a-practical-guide-to-be-highly-effective-r-software-and-data-visualization