Create Dot Charts in R Programming – dotchart () Function

dotchart () Function in detail

The dotchart() function in R is used to create a Cleveland dot plot, where individual data points are represented as dots. This type of plot is particularly useful for comparing a set of numerical values along an axis, with optional grouping and labeling for enhanced clarity.

Syntax

dotchart(x, labels = NULL, groups = NULL, gcolor = par("fg"), color = par("fg"), ...)
  • x: A numeric vector or matrix.
  • labels: A vector of labels for each point.
  • groups: A grouping variable to indicate how the elements of x are categorized.
  • gcolor: Color(s) used for group labels and group values.
  • color: Color(s) used for the individual points and their labels.

Example 1: Dot Chart of a Single Numeric Vector

This example demonstrates how to create a dot chart for a numeric vector representing exam scores, with each score labeled by a student’s name.

# Example 1: Dot Chart for Exam Scores
# Create a numeric vector for exam scores
scores <- c(85, 90, 78, 92, 88, 75, 95)

# Create labels for each student
student_names <- c("Alice", "Bob", "Charlie", "David", "Eva", "Frank", "Grace")

# Generate the dot chart
dotchart(scores, labels = student_names,
         cex = 0.9, xlab = "Exam Score",
         main = "Dot Chart of Exam Scores")

Output:

Example 2: Dot Chart with Grouping

In this example, a numeric vector representing monthly sales is grouped into two categories (e.g., “First Half” and “Second Half” of the year). Different colors are used to distinguish the groups.

# Example 2: Dot Chart Grouped by Period
# Create a numeric vector for monthly sales figures
sales <- c(120, 150, 180, 130, 160, 170, 140, 190, 200, 155)

# Create labels for each month
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct")

# Define a grouping variable: "First Half" for the first 5 months and "Second Half" for the rest
group <- factor(ifelse(1:10 <= 5, "First Half", "Second Half"))

# Define colors for the two groups
group_colors <- c("purple", "teal")

# Generate the dot chart with grouping
dotchart(sales, labels = months, groups = group,
         gcolor = group_colors,
         color = group_colors[as.numeric(group)],
         cex = 0.9, pch = 21, xlab = "Monthly Sales",
         main = "Monthly Sales Dot Chart by Group")

Output:

Example 3: Dot Chart with Custom Labels and Titles

This example shows how to create a dot chart for a series of measurements with custom point labels and axis titles.

# Example 3: Dot Chart for Measurements
# Create a numeric vector of measurement values
measurements <- c(5.2, 7.8, 6.1, 8.5, 7.0, 6.8, 9.2)

# Create custom labels for each measurement
labels <- c("P1", "P2", "P3", "P4", "P5", "P6", "P7")

# Generate the dot chart
dotchart(measurements, labels = labels,
         main = "Dot Chart of Measurements",
         xlab = "Measurement Value", ylab = "Points",
         cex = 0.9)

Output:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *