How to Create a Plot in R
Learn how to create visualizations in R using base plotting and ggplot2.
R offers powerful tools for data visualization. Learn how to create various types of plots using base R and ggplot2.
Base R Plotting
Simple Scatter Plot
# Create sample data
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 5, 4, 6)
# Basic scatter plot
plot(x, y)
# With customization
plot(x, y,
main = "My Scatter Plot",
xlab = "X Axis",
ylab = "Y Axis",
col = "blue",
pch = 19) # Solid circles
Line Plot
# Line plot
plot(x, y, type = "l", col = "red")
# Points and lines
plot(x, y, type = "b", col = "blue")
# Multiple lines
plot(x, y, type = "l", col = "blue")
lines(x, y * 1.5, col = "red")
legend("topleft", legend = c("Series 1", "Series 2"),
col = c("blue", "red"), lty = 1)
Bar Plot
# Simple bar plot
values <- c(3, 5, 2, 8, 4)
names <- c("A", "B", "C", "D", "E")
barplot(values,
names.arg = names,
main = "Bar Chart",
col = "steelblue")
# Horizontal bars
barplot(values, names.arg = names, horiz = TRUE)
Histogram
# Generate random data
data <- rnorm(1000, mean = 50, sd = 10)
# Histogram
hist(data,
main = "Distribution",
xlab = "Values",
col = "lightblue",
breaks = 30)
ggplot2 Basics
ggplot2 uses a grammar of graphics approach:
library(ggplot2)
# Create a data frame
df <- data.frame(
x = c(1, 2, 3, 4, 5),
y = c(2, 4, 5, 4, 6),
group = c("A", "A", "B", "B", "B")
)
# Basic scatter plot
ggplot(df, aes(x = x, y = y)) +
geom_point()
# With customization
ggplot(df, aes(x = x, y = y, color = group)) +
geom_point(size = 3) +
labs(title = "Scatter Plot",
x = "X Axis",
y = "Y Axis") +
theme_minimal()
Common ggplot2 Visualizations
Scatter Plot with Trend Line
ggplot(df, aes(x = x, y = y)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red")
Bar Chart
# Create data
sales <- data.frame(
product = c("A", "B", "C", "D"),
revenue = c(100, 150, 80, 200)
)
ggplot(sales, aes(x = product, y = revenue)) +
geom_bar(stat = "identity", fill = "steelblue") +
labs(title = "Sales by Product")
Line Chart
# Time series data
time_data <- data.frame(
month = 1:12,
value = c(10, 12, 15, 18, 22, 25, 28, 26, 23, 18, 14, 11)
)
ggplot(time_data, aes(x = month, y = value)) +
geom_line(color = "blue", size = 1) +
geom_point(color = "blue", size = 2) +
scale_x_continuous(breaks = 1:12) +
labs(title = "Monthly Trends")
Histogram
# Generate data
df <- data.frame(values = rnorm(1000, mean = 50, sd = 10))
ggplot(df, aes(x = values)) +
geom_histogram(bins = 30, fill = "lightblue", color = "black") +
labs(title = "Distribution of Values")
Box Plot
# Create grouped data
df <- data.frame(
group = rep(c("A", "B", "C"), each = 100),
value = c(rnorm(100, 10), rnorm(100, 15), rnorm(100, 12))
)
ggplot(df, aes(x = group, y = value, fill = group)) +
geom_boxplot() +
labs(title = "Distribution by Group")
Customizing ggplot2
Themes
# Built-in themes
ggplot(df, aes(x = x, y = y)) +
geom_point() +
theme_minimal() # or theme_bw(), theme_classic(), theme_dark()
# Custom theme elements
ggplot(df, aes(x = x, y = y)) +
geom_point() +
theme(
plot.title = element_text(size = 16, face = "bold"),
axis.text = element_text(size = 12),
panel.background = element_rect(fill = "white")
)
Colors and Scales
# Custom colors
ggplot(df, aes(x = x, y = y, color = group)) +
geom_point(size = 3) +
scale_color_manual(values = c("A" = "red", "B" = "blue"))
# Color gradients
ggplot(df, aes(x = x, y = y, color = y)) +
geom_point(size = 3) +
scale_color_gradient(low = "blue", high = "red")
Faceting
Create multiple plots:
ggplot(df, aes(x = x, y = y)) +
geom_point() +
facet_wrap(~ group) # Separate plot for each group
Saving Plots
# Save ggplot
p <- ggplot(df, aes(x = x, y = y)) + geom_point()
ggsave("my_plot.png", p, width = 8, height = 6, dpi = 300)
# Save base R plot
png("my_plot.png", width = 800, height = 600)
plot(x, y)
dev.off()
Summary
- Use base R
plot()for quick visualizations - Use ggplot2 for publication-quality graphics
- ggplot2 syntax:
ggplot(data, aes()) + geom_*() - Customize with
labs(),theme(), andscale_*()functions - Save plots with
ggsave()orpng()/pdf()+dev.off()