R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Adding titles and labels to graphs is crucial for providing context to the viewer. A descriptive title can quickly convey the purpose of the graph. Similarly, axis labels explain the variables being plotted. In this tutorial, we'll show how to add titles and axis labels to plots in R using both base graphics and ggplot2
.
In base graphics, you can add titles and labels directly in the plot()
function or by using additional functions like title()
.
plot()
:x <- 1:10 y <- rnorm(10) plot(x, y, main="Sample Scatter Plot", xlab="X-Axis Label", ylab="Y-Axis Label")
Here:
main
: Specifies the main title.xlab
: Specifies the label for the x-axis.ylab
: Specifies the label for the y-axis.title()
:If you want to add or modify titles after generating the plot, you can use the title()
function:
plot(x, y) title(main="Sample Scatter Plot", xlab="X-Axis Label", ylab="Y-Axis Label", col.main="red", col.lab="blue")
col.main
: Specifies the color of the main title.col.lab
: Specifies the color of the axis labels.In ggplot2
, you add titles and labels using the labs()
function or specific functions like ggtitle()
, xlab()
, and ylab()
.
labs()
:library(ggplot2) data <- data.frame(x, y) ggplot(data, aes(x, y)) + geom_point() + labs(title="Sample Scatter Plot", x="X-Axis Label", y="Y-Axis Label")
ggtitle()
, xlab()
, and ylab()
:These functions allow you to set titles and labels individually:
ggplot(data, aes(x, y)) + geom_point() + ggtitle("Sample Scatter Plot") + xlab("X-Axis Label") + ylab("Y-Axis Label")
Subtitle and Captions: In ggplot2
, you can also add subtitles and captions using the labs()
function:
ggplot(data, aes(x, y)) + geom_point() + labs(title="Sample Scatter Plot", subtitle="This is a subtitle", caption="Source: Sample Data")
Styling Titles: In ggplot2
, you can style the titles using theme()
:
ggplot(data, aes(x, y)) + geom_point() + ggtitle("Sample Scatter Plot") + theme(plot.title=element_text(face="bold", color="blue"))
Titles and labels are indispensable for making plots informative. Whether using base graphics or ggplot2
, R provides you with a comprehensive set of tools to ensure your graphs convey information effectively.
R Graph Add Title Example:
# Create a simple plot plot(1:10, main = "Title Example", col = "blue", pch = 16)
How to Add Titles to Plots in R:
# Create a scatter plot plot(mtcars$mpg, mtcars$hp, main = "Scatter Plot: MPG vs HP", col = "red", pch = 16)
Customizing Main Titles in R Graphs:
# Create a line plot plot(1:10, type = "l", col = "green", lwd = 2, main = list("Customized Main Title", cex.main = 1.5))
Adding Subtitles to R Plots:
# Create a bar plot barplot(1:10, main = "Bar Plot", sub = "Subtitle: 1 to 10", col = "purple")
Positioning Titles in R Graphs:
# Create a histogram hist(rnorm(100), main = list("Histogram", cex.main = 1.2), col = "orange")
Titles and Labels in ggplot2 in R:
# Create a ggplot scatter plot library(ggplot2) ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point() + ggtitle("Scatter Plot: MPG vs HP") + theme_minimal()
Adding Captions to R Figures:
# Create a boxplot boxplot(mtcars$mpg ~ mtcars$cyl, main = "Boxplot: MPG by Cylinder", col = "brown") mtext("Figure 1: Boxplot of MPG by Cylinder", side = 1, line = 3)
Custom Font Styles for Graph Titles in R:
# Create a pie chart pie(1:5, main = list("Pie Chart", cex.main = 1.2, font.main = 2), col = rainbow(5))
Adding Titles to Multiple Plots in R:
# Create two plots side by side par(mfrow = c(1, 2)) plot(1:10, main = "Plot 1", col = "blue") plot(10:1, main = "Plot 2", col = "red") par(mfrow = c(1, 1)) # Reset to single plot layout
Interactive Titles in R Graphs:
# Create an interactive scatter plot using plotly library(plotly) plot_ly(x = mtcars$mpg, y = mtcars$hp, type = "scatter", mode = "markers") %>% layout(title = "Interactive Scatter Plot: MPG vs HP")