Next →

Output:


      

Plots:

Data Visualization with ggplot2: Basics

ggplot2 is a powerful R package for creating elegant and customizable visualizations. It uses a layered approach: you start with a dataset and map variables to visual properties like axes, colors, or shapes.

Creating a Basic Scatter Plot

library(ggplot2)

# Sample data
df <- data.frame(
  height = c(150, 160, 170, 180, 190),
  weight = c(55, 60, 65, 70, 75)
)

# Create a scatter plot
ggplot(df, aes(x = height, y = weight)) +
  geom_point()

Creating a Basic Line Plot

# Sample data
time <- 1:5
value <- c(10, 12, 15, 18, 20)
df_line <- data.frame(time, value)

# Create a line plot
ggplot(df_line, aes(x = time, y = value)) +
  geom_line()

These examples show how to quickly visualize relationships between variables using points or lines.