0
0
R Programmingprogramming~5 mins

Labels and titles in R Programming

Choose your learning style9 modes available
Introduction

Labels and titles help explain what your graphs or charts show. They make your data easier to understand.

When you create a plot and want to name the x-axis and y-axis.
When you want to add a main title to your graph to explain what it represents.
When you want to add a subtitle or caption to give extra information about the plot.
When you share your graph with others and want them to quickly understand the data.
Syntax
R Programming
plot(x, y, main = "Main Title", xlab = "X-axis Label", ylab = "Y-axis Label")

main adds the main title to the plot.

xlab and ylab add labels to the x-axis and y-axis.

Examples
This creates a simple line plot with a main title and axis labels.
R Programming
plot(1:5, 1:5, main = "Simple Line", xlab = "Index", ylab = "Value")
A bar chart with a title and labels for the bars and counts.
R Programming
barplot(c(3, 7, 2), main = "Bar Chart", xlab = "Categories", ylab = "Counts")
Plot with only a main title, no axis labels.
R Programming
plot(1:10, (1:10)^2, main = "Square Numbers")
Sample Program

This program plots points from x and y with a main title and axis labels to explain the data.

R Programming
x <- 1:5
 y <- c(2, 4, 6, 8, 10)
 plot(x, y, main = "My Line Plot", xlab = "X Values", ylab = "Y Values")
OutputSuccess
Important Notes

You can add subtitles and captions using title(sub = "Subtitle") and mtext("Caption", side = 1).

Labels and titles improve the clarity and professionalism of your graphs.

Summary

Labels name the axes to show what data they hold.

Titles explain what the whole graph is about.

Use main, xlab, and ylab to add these in R plots.