Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

R ggplot2 Jitter

by suresh

The R ggplot2 Jitter is very useful to handle the overplotting caused by the smaller datasets discreteness. Let us see how to plot a ggplot jitter, Format its color, change the labels, adding boxplot, violin plot, and alter the legend position using R ggplot2 with example.

The syntax to draw a ggplot jitter in R Programming is

geom_jitter(data = NULL, width = NULL, height = NULL)

and the complex syntax behind this Jitter is:

geom_jitter(mapping = NULL, data = NULL, stat = "identity",
            width = NULL, height = NULL, position = "jitter", ...,
            na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

Create R ggplot2 Jitter

Let us see how to plot the Jitter using the ggplot2 package, and we are going to use the ChickWeight data set, provided by the Studio.

TIP: The ggplot2 package not installed by default. Please refer Install R Packages article to install the package in R Programming.

# Create R ggplot Jittering

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight)) + 
  geom_jitter()

OUTPUT

R ggplot2 Jitter 1

NOTE: If you require to import data from external files, then please refer R Read CSV article to import the CSV file.

Change Colors of a Jiiter in R

Let us see how to change the R Jitter plot dot colors using theĀ color argument

# Change Color of a R ggplot Jitter

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight)) + 
  geom_jitter(aes(colour = Diet))

OUTPUT

R ggplot2 Jitter 2

Position adjustment in a ggplot2 Jitter

Here, we show how to adjust the dot positions using position argument

# Position adjustment of a R ggplot Jitter

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight)) + 
  geom_jitter(position = position_jitter(0.5), 
              aes(colour = Diet))

OUTPUT

R ggplot2 Jitter 3

Change Size, and Shape of a dot in a R ggplot2 Jitter

How to change the size of the character that represents the point using cex argument, and shape of the character using shape argument.

  • cex: Please specify the size of the point(s)
# Change Shape & Size of a Point in a R ggplot Jitter

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight)) + 
  geom_jitter(position = position_jitter(0.5), 
              aes(colour = Diet),
              cex = 1.8, shape = 8)

OUTPUT

R ggplot2 Jitter 4

Change Width, and Height of a R ggplot2 Jitter

In this example, we show how to change the width and height of the jitter plot

# Change Width & Height of a R ggplot Jitter

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight)) + 
  geom_jitter(aes(colour = Diet),
              width = 1.4, height = 500)

OUTPUT

R ggplot2 Jitter 5

Add Mean & Median to R ggplot2 Jitter

Let us see how to add Mean, and Median value toggplot2 Jitter plot using stat_summary function.

# Add Mean & Median to R ggplot Jitter

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight)) + 
  geom_jitter(position = position_jitter(0.5), aes(colour = Diet)) +
  stat_summary(fun.y = "mean", geom = "point", 
               shape = 8, size = 3, color = "darkorchid4" ) +
  stat_summary(fun.y = "median", geom = "point", 
               shape = 2, size = 3, color = "mediumvioletred")

OUTPUT

R ggplot2 Jitter 6

Add Boxplot to R ggplot2 Jitter

In this example, we add boxplot to R Jitter using geom_boxplot function. It help us to see the Median, along with the quartile for our violin plot.

TIP: Please refer R ggplot2 Boxplot article to understand the Boxplot arguments.

# Add Boxplot to R ggplot Jitter

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight)) + 
  geom_boxplot() +
  geom_jitter(position = position_jitter(0.5), aes(colour = Diet))

OUTPUT

R ggplot2 Jitter 7

Add Boxplot to R ggplot2 jitter 2

In this example, we draw a line on each side of the boxes using notch argument

# Add Boxplot to R ggplot Jitter

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight)) + 
  geom_boxplot(notch = TRUE) +
  geom_jitter(position = position_jitter(0.5), aes(colour = Diet))

OUTPUT

R ggplot2 Jitter 8

Add Violin plot to R ggplot2 Jitter

In this example, we show how to add Violin Plot to Jitter using geom_violin function.

TIP: Please refer R ggplot2 Violin Plot article to understand the Violin plot arguments.

# Add Violin Plot to R ggplot Jitter

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight, color = Diet)) + 
  geom_violin(fill = "midnightblue") +
  geom_jitter(position = position_jitter(0.2)) 

OUTPUT

R ggplot2 Jitter 9

Alter Legend position of a ggplot2 Jitter

By default, ggplot position the legend at the right side of a Jitter. Let us see how to change the legend position from right to top. Note, You can use legend.position = “none” to completely remove the legend.

# Alter Legend Position in R ggplot Jitter

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight, color = Diet)) + 
  geom_violin(fill = "midnightblue") +
  geom_jitter(position = position_jitter(0.2)) +
  theme(legend.position = "top")

OUTPUT

R ggplot2 Jitter 10

Assigning names to ggplot2 Jitter

How to assign names to ggplot jitter plot, X-Axis, and Y-Axis using labs function?

# Alter Legend Position in R ggplot Jitter

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight, color = Diet)) + 
  geom_violin(fill = "midnightblue") +
  geom_jitter(position = position_jitter(0.2)) +
  theme(legend.position = "top")

OUTPUT

R ggplot2 Jitter 11

Change R ggplot2 Jitter Theme

In this example, we show how to change the default theme of a ggplot jitter

  • theme_dark(): We use this function to change the jitter default theme to dark. If you type theme_ then R Studio intelligence will show you the list of available options. For example, theme_grey()
# Change the Theme of a R ggplot Jitter

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Jitter
ggplot(ChickWeight, aes(x = Diet, y = weight, color = Diet)) + 
  geom_violin(fill = "midnightblue") +
  geom_jitter(position = position_jitter(0.2)) +
  labs(title="GGPLOT JITTER", x="Chick Diet", y="Weight") +
  theme_dark()

OUTPUT

R ggplot2 Jitter 12

Placed Under: R Programming

  • R Software Download
  • Install R Packages
  • Install R Software
  • Download & Install R Studio
  • R Arithmetic Operators
  • R Comparison Operators
  • R Logical Operators
  • R If Statement
  • R If Else Statement
  • R Else If Statement
  • R Nested If Else
  • R Switch Statement
  • R Break Statement
  • R Next Statement
  • R For Loop
  • R While Loop
  • R Repeat
  • R Vector
  • R Data Frame
  • R List
  • R Arrays
  • R Matrix
  • SQL Server R Services
  • R Read CSV Function
  • R Read table Function
  • R Barplot
  • R Boxplot
  • R Histogram
  • R Pie Chart
  • R Stacked Barplot
  • R Stem and Leaf Plot
  • R Mosaic Plot
  • R ggplot2 Boxplot
  • R ggplot2 Density Plot
  • R ggplot2 Dot Plot
  • R ggplot2 Histogram
  • R ggplot2 Jitter
  • R ggplot2 Line Plot
  • R ggplot2 Scatter Plot
  • R ggplot2 Violin Plot
  • Save R ggplot using ggsave
  • R Lattice Bar chart
  • R Lattice Scatter Plot
  • R Lattice Histogram
  • R Functions
  • R Recursive Functions
  • R abs Function
  • R sqrt Function
  • R ceiling Function
  • R floor Function
  • R round Function
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy