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
When presenting data in graphical form, understanding the key components of a chart is crucial for both the creator and the viewer. Different charts may have various elements, but most share a common set of components.
In this tutorial, we'll discuss and visualize the main parts of a typical chart using R's base plotting functions.
You don't need any additional packages for this tutorial, just base R.
Let's create a simple scatter plot using plot()
to illustrate the components:
# Sample data x <- c(1,2,3,4,5) y <- c(5,4,3,2,1) # Plotting plot(x, y, main="Title of the Chart", xlab="X-Axis Label", ylab="Y-Axis Label", xlim=c(0,6), ylim=c(0,6))
This is the heading at the top of the graph.
title("Title of the Chart")
These are the horizontal and vertical lines forming the chart's grid.
Labels that describe the data represented on each axis.
xlabel <- "X-Axis Label" ylabel <- "Y-Axis Label" axis(1, at=seq(0,6,by=1), lab=seq(0,6,by=1)) axis(2, at=seq(0,6,by=1), lab=seq(0,6,by=1)) mtext(xlabel, side=1, line=3) mtext(ylabel, side=2, line=3)
The actual data points being represented on the graph. In a scatter plot, these are the dots.
points(x, y, pch=19, col="blue")
These are the lines that form a grid on the plot area, helping to read values more easily.
grid()
A small section that describes the symbols or colors used in the chart. Especially useful when multiple datasets are plotted.
legend("topright", legend="Sample Data", pch=19, col="blue")
This is the area bounded by the axes where the data is plotted.
Here's how you can combine all of the components together:
plot(x, y, main="Title of the Chart", xlab="X-Axis Label", ylab="Y-Axis Label", xlim=c(0,6), ylim=c(0,6), pch=19, col="blue", type="p") grid() legend("topright", legend="Sample Data", pch=19, col="blue")
Recognizing these fundamental components will make it easier to interpret charts and design clear and effective visual representations of data. Whether you're working with scatter plots, bar graphs, histograms, or any other type of chart, many of these elements will be present and serve to enhance clarity and comprehension.
Labeling parts of a plot in R:
# Labeling parts of a plot in R x <- 1:5 y <- c(3, 7, 1, 8, 5) plot(x, y, main = "Labeling Parts of a Plot") text(3, 8, "Important Point", col = "red", cex = 1.2)
Adding text to graphs in R:
# Adding text to graphs in R x <- 1:5 y <- c(3, 7, 1, 8, 5) plot(x, y, main = "Adding Text to Graphs") text(2, 7, "Additional Information", col = "blue", cex = 1.2)
Title, labels, and legends in R plots:
# Title, labels, and legends in R plots x <- 1:5 y <- c(3, 7, 1, 8, 5) plot(x, y, main = "Plot with Title and Labels", xlab = "X-axis", ylab = "Y-axis") legend("topright", legend = "Legend", col = "red", lty = 1)
R annotate package for charts:
annotate
package in R allows for additional annotations on charts.# Using annotate package for annotations library(annotate) x <- 1:5 y <- c(3, 7, 1, 8, 5) plot(x, y, main = "Annotate Package Example") annotate_text(3, 8, label = "Annotated Point", color = "purple", size = 4)
Describing chart components in R:
# Describing chart components in R x <- 1:5 y <- c(3, 7, 1, 8, 5) plot(x, y, main = "Describing Chart Components") title(main = "Main Title", xlab = "X-axis Label", ylab = "Y-axis Label")
Adding annotations to ggplot2 in R:
ggplot2
for creating annotated plots.# Adding annotations to ggplot2 in R library(ggplot2) df <- data.frame(x = 1:5, y = c(3, 7, 1, 8, 5)) ggplot(df, aes(x, y)) + geom_point() + annotate("text", x = 3, y = 8, label = "Annotated Point", color = "green", size = 4)
Customizing axis labels in R plots:
# Customizing axis labels in R plots x <- 1:5 y <- c(3, 7, 1, 8, 5) plot(x, y, main = "Customizing Axis Labels", xlab = "X-axis Label", ylab = "Y-axis Label", col.axis = "blue")
Legends and keys in R graphical representation:
# Legends and keys in R graphical representation x <- 1:5 y <- c(3, 7, 1, 8, 5) plot(x, y, main = "Legends and Keys") legend("topright", legend = "Legend", col = "red", lty = 1)