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
Using base R, plotting is straightforward and doesn't require any additional packages. Let's go over a simple tutorial on how to plot data using R's generic plotting functions.
To start, let's plot a simple line graph.
# Generate data x <- seq(0, 10, 0.1) y <- sin(x) # Plot plot(x, y, type="l", main="Sin Wave", xlab="X Axis", ylab="Y Axis")
The type="l"
argument tells R to plot lines.
You can create scatter plots using the same plot
function.
# Generate data x <- rnorm(100) # 100 random values from a standard normal distribution y <- rnorm(100) # Plot plot(x, y, main="Scatter Plot", xlab="X Axis", ylab="Y Axis")
# Generate data labels <- c("A", "B", "C", "D") values <- c(23, 45, 10, 50) # Plot barplot(values, names.arg=labels, main="Bar Plot", xlab="Categories", ylab="Value")
Histograms are used to show the distribution of a set of continuous data.
# Generate data data <- rnorm(500) # Plot hist(data, main="Histogram", xlab="Value", breaks=20)
The breaks
argument determines how many bins (or intervals) the data should be divided into.
Box plots (or box-and-whisker plots) display the distribution of data based on a five-number summary: minimum, first quartile, median, third quartile, and maximum.
# Generate data group1 <- rnorm(50, mean=0) group2 <- rnorm(50, mean=1) data <- list(Group1=group1, Group2=group2) # Plot boxplot(data, main="Box Plot", ylab="Value")
# Generate data labels <- c("A", "B", "C", "D") values <- c(25, 35, 15, 25) # Plot pie(values, labels=labels, main="Pie Chart")
Almost every aspect of a base R plot can be customized, from the type and color of the plotting character to the plot axes and labels. Some examples:
# Generate data x <- rnorm(100) y <- rnorm(100) # Customized plot plot(x, y, main="Customized Scatter Plot", xlab="X Axis", ylab="Y Axis", col="red", # Color of points pch=16, # Type of point cex=1.5, # Point size xlim=c(-3, 3), # X axis limits ylim=c(-3, 3)) # Y axis limits
These are just the basics, and there are many more features and customization options available with R's base plotting system. Once you're comfortable with these basics, you can explore more advanced plotting systems in R like ggplot2
, lattice
, and others.
Creating basic plots in R programming:
Overview: Introduce the fundamental concepts of creating plots in R.
Code:
# Creating basic plots in R programming x <- c(1, 2, 3, 4, 5) y <- c(3, 5, 2, 8, 6) # Scatter plot plot(x, y, main = "Basic Scatter Plot", xlab = "X-axis", ylab = "Y-axis", col = "blue", pch = 16)
Customizing generic plots in R:
Overview: Demonstrate how to customize basic plots using additional parameters.
Code:
# Customizing generic plots in R x <- c(1, 2, 3, 4, 5) y <- c(3, 5, 2, 8, 6) # Bar plot with customizations barplot(y, names.arg = x, main = "Customized Bar Plot", xlab = "X-axis", ylab = "Y-axis", col = "green")
Plotting data with generic plot types in R:
Overview: Provide examples of different plot types available with generic functions.
Code:
# Plotting data with generic plot types in R x <- c(1, 2, 3, 4, 5) y <- c(3, 5, 2, 8, 6) # Box plot boxplot(y ~ x, main = "Box Plot", xlab = "X-axis", ylab = "Y-axis", col = "orange")
Exploratory data analysis with generic plots in R:
Overview: Illustrate how generic plots facilitate exploratory data analysis.
Code:
# Exploratory data analysis with generic plots in R data <- iris # Pairwise scatter plots pairs(data[, 1:4], main = "Pairwise Scatter Plots", col = data$Species)