Introduction
Base R graphics provide a powerful and flexible system for creating a wide variety of plots and charts. This module will cover the basics of creating and customizing plots using base R functions. By the end of this section, you will be able to create simple plots, customize them, and understand the different types of plots available in base R.
Key Concepts
- Plotting Functions: Functions like
plot()
,hist()
,boxplot()
, etc. - Customizing Plots: Adding titles, labels, legends, and customizing colors and symbols.
- Types of Plots: Scatter plots, line plots, bar plots, histograms, box plots, etc.
Basic Plotting Functions
Scatter Plot
The plot()
function is one of the most versatile plotting functions in base R. It can be used to create scatter plots, line plots, and more.
# Example: Scatter Plot x <- rnorm(100) y <- rnorm(100) plot(x, y, main="Scatter Plot", xlab="X-axis", ylab="Y-axis", col="blue", pch=19)
Explanation:
x
andy
are vectors of data points.main
sets the title of the plot.xlab
andylab
set the labels for the x and y axes.col
sets the color of the points.pch
sets the plotting character (symbol) for the points.
Line Plot
To create a line plot, you can use the type
argument in the plot()
function.
# Example: Line Plot x <- seq(1, 10, by=1) y <- x^2 plot(x, y, type="l", main="Line Plot", xlab="X-axis", ylab="Y-axis", col="red")
Explanation:
type="l"
specifies that the plot should be a line plot.
Bar Plot
The barplot()
function is used to create bar plots.
# Example: Bar Plot counts <- c(5, 10, 15, 20) barplot(counts, main="Bar Plot", xlab="Categories", ylab="Counts", col="green", names.arg=c("A", "B", "C", "D"))
Explanation:
counts
is a vector of bar heights.names.arg
sets the names for the bars.
Histogram
The hist()
function is used to create histograms.
# Example: Histogram data <- rnorm(1000) hist(data, main="Histogram", xlab="Values", col="purple", border="black")
Explanation:
data
is a vector of values to be plotted in the histogram.border
sets the color of the border around the bars.
Box Plot
The boxplot()
function is used to create box plots.
# Example: Box Plot data <- list(A=rnorm(100), B=rnorm(100, mean=1), C=rnorm(100, mean=2)) boxplot(data, main="Box Plot", xlab="Groups", ylab="Values", col=c("orange", "yellow", "pink"))
Explanation:
data
is a list of numeric vectors.col
sets the colors for the boxes.
Customizing Plots
Adding Titles and Labels
You can add titles and labels to your plots using the title()
function.
# Example: Adding Titles and Labels plot(x, y) title(main="Main Title", sub="Subtitle", xlab="X-axis Label", ylab="Y-axis Label")
Adding Legends
The legend()
function is used to add legends to your plots.
# Example: Adding Legends plot(x, y, col="blue", pch=19) points(x, y+1, col="red", pch=17) legend("topright", legend=c("Group 1", "Group 2"), col=c("blue", "red"), pch=c(19, 17))
Explanation:
legend
specifies the position of the legend.legend
argument specifies the text for the legend.col
andpch
specify the colors and symbols for the legend.
Customizing Colors and Symbols
You can customize the colors and symbols in your plots using various arguments in the plotting functions.
Explanation:
col=rainbow(100)
sets a rainbow color palette for the points.pch=16
sets the plotting character to a filled circle.
Practical Exercises
Exercise 1: Create a Scatter Plot
Create a scatter plot using the plot()
function with the following data:
x <- rnorm(50)
y <- rnorm(50)
Add a title, x and y labels, and customize the color and symbol of the points.
Solution:
x <- rnorm(50) y <- rnorm(50) plot(x, y, main="Scatter Plot Exercise", xlab="X-axis", ylab="Y-axis", col="darkgreen", pch=18)
Exercise 2: Create a Histogram
Create a histogram using the hist()
function with the following data:
data <- rnorm(500)
Add a title, x label, and customize the color of the bars.
Solution:
data <- rnorm(500) hist(data, main="Histogram Exercise", xlab="Values", col="skyblue", border="white")
Exercise 3: Create a Box Plot
Create a box plot using the boxplot()
function with the following data:
data <- list(Group1=rnorm(50), Group2=rnorm(50, mean=2), Group3=rnorm(50, mean=4))
Add a title, x and y labels, and customize the colors of the boxes.
Solution:
data <- list(Group1=rnorm(50), Group2=rnorm(50, mean=2), Group3=rnorm(50, mean=4)) boxplot(data, main="Box Plot Exercise", xlab="Groups", ylab="Values", col=c("lightblue", "lightgreen", "lightcoral"))
Common Mistakes and Tips
- Mistake: Forgetting to label axes and add titles.
- Tip: Always add meaningful titles and labels to make your plots more informative.
- Mistake: Overlapping points in scatter plots.
- Tip: Use transparency (
alpha
parameter) or jittering to avoid overlapping points.
- Tip: Use transparency (
- Mistake: Using too many colors or symbols.
- Tip: Keep your plots simple and avoid using too many different colors or symbols.
Conclusion
In this section, you learned how to create and customize various types of plots using base R graphics. You should now be able to create scatter plots, line plots, bar plots, histograms, and box plots, and customize them with titles, labels, legends, colors, and symbols. In the next module, we will explore more advanced data visualization techniques using the ggplot2
package.
R Programming: From Beginner to Advanced
Module 1: Introduction to R
- Introduction to R and RStudio
- Basic R Syntax
- Data Types and Structures
- Basic Operations and Functions
- Importing and Exporting Data
Module 2: Data Manipulation
- Vectors and Lists
- Matrices and Arrays
- Data Frames
- Factors
- Data Manipulation with dplyr
- String Manipulation
Module 3: Data Visualization
- Introduction to Data Visualization
- Base R Graphics
- ggplot2 Basics
- Advanced ggplot2
- Interactive Visualizations with plotly
Module 4: Statistical Analysis
- Descriptive Statistics
- Probability Distributions
- Hypothesis Testing
- Correlation and Regression
- ANOVA and Chi-Square Tests
Module 5: Advanced Data Handling
Module 6: Advanced Programming Concepts
- Writing Functions
- Debugging and Error Handling
- Object-Oriented Programming in R
- Functional Programming
- Parallel Computing
Module 7: Machine Learning with R
- Introduction to Machine Learning
- Data Preprocessing
- Supervised Learning
- Unsupervised Learning
- Model Evaluation and Tuning
Module 8: Specialized Topics
- Time Series Analysis
- Spatial Data Analysis
- Text Mining and Natural Language Processing
- Bioinformatics with R
- Financial Data Analysis