library(ggplot2)
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
position_dodgefunction.step 1:- load the required library
library(ggplot2)
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
step 2:-convert dose to factor
# Convert dose to a factor
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# Load ggplot2
library(ggplot2)
# Create the dot plot
ggplot(ToothGrowth, aes(x = dose, y = len, color = supp)) +
geom_dotplot(
binaxis = "y",
stackdir = "center",
dotsize = 0.6,
position = position_dodge(0.8),
binwidth = 1.5
) +
labs(
title = "Tooth Growth by Supplement Type and Dose",
x = "Dose (mg)",
y = "Tooth Length"
) +
theme_minimal()