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
Graph plotting is one of R's strongest features, thanks to the extensive variety of plotting functions and libraries available. This tutorial will introduce you to base plotting functions and then touch upon the popular ggplot2
package for a more sophisticated plotting experience.
1.1 Basic Plot: The basic plot function can be used to create scatter plots.
# Create data x <- rnorm(50) y <- rnorm(50) + x # Basic scatter plot plot(x, y, main="Scatter Plot", xlab="X-axis", ylab="Y-axis")
1.2 Line Plot:
You can add a line to a scatter plot using the lines
function.
# Create data x <- seq(1, 10, by=0.5) y <- sin(x) # Line plot plot(x, y, type="l", main="Line Plot", xlab="X-axis", ylab="Y-axis")
1.3 Histogram:
# Create data data <- rnorm(1000) # Histogram hist(data, main="Histogram", xlab="Values", col="skyblue", border="black")
To use ggplot2
, you'll need to install and load it:
install.packages("ggplot2") library(ggplot2)
2.1 Basic Scatter Plot:
df <- data.frame(x = rnorm(50), y = rnorm(50) + x) ggplot(df, aes(x=x, y=y)) + geom_point(aes(color=x)) + ggtitle("Scatter Plot with ggplot2") + xlab("X-axis") + ylab("Y-axis")
2.2 Histogram:
data <- data.frame(values = rnorm(1000)) ggplot(data, aes(x=values)) + geom_histogram(fill="skyblue", color="black", binwidth=0.5) + ggtitle("Histogram with ggplot2") + xlab("Values")
2.3 Bar Plot: Suppose you have some categorical data:
data <- data.frame( category = c("A", "B", "C", "D"), count = c(23, 45, 10, 50) ) ggplot(data, aes(x=category, y=count)) + geom_bar(stat="identity", fill="coral") + ggtitle("Bar Plot with ggplot2") + xlab("Category") + ylab("Count")
2.4 Faceting: Faceting is a technique that allows you to split one plot into multiple plots based on a factor variable.
df <- data.frame( x = rnorm(100), y = rnorm(100), category = rep(letters[1:2], each=50) ) ggplot(df, aes(x=x, y=y)) + geom_point() + facet_wrap(~category)
This tutorial is a concise introduction to plotting in R. Base R provides a solid foundation for plotting, but ggplot2
elevates the plotting experience with a consistent and comprehensive framework for creating complex plots from data.
Graph plotting in R:
plot()
), ggplot2, lattice, and others.# Basic graph plotting in R x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) plot(x, y, main = "Simple Scatter Plot", xlab = "X-axis", ylab = "Y-axis")
Creating graphs in R:
# Creating a simple scatter plot in R x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) plot(x, y, main = "Simple Scatter Plot", xlab = "X-axis", ylab = "Y-axis")
R base graphics:
plot()
, barplot()
, and lines()
for creating basic plots without additional packages.# Using base graphics for a scatter plot x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) plot(x, y, main = "Base Graphics Scatter Plot", xlab = "X-axis", ylab = "Y-axis")
Scatter plot in R:
# Creating a scatter plot in R x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) plot(x, y, main = "Scatter Plot", xlab = "X-axis", ylab = "Y-axis")
Bar chart in R:
# Creating a bar chart in R categories <- c("A", "B", "C", "D") values <- c(3, 6, 2, 8) barplot(values, names.arg = categories, main = "Bar Chart", xlab = "Categories", ylab = "Values")
Line plot in R:
# Creating a line plot in R x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) plot(x, y, type = "l", main = "Line Plot", xlab = "X-axis", ylab = "Y-axis")
R plot function examples:
plot()
function in R is versatile and can be used for various types of plots. Examples include scatter plots, line plots, and more.# Examples of using the plot function in R x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) plot(x, y, main = "Example Plot", xlab = "X-axis", ylab = "Y-axis")
Customizing plots in R:
# Customizing a scatter plot in R x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) plot(x, y, main = "Customized Scatter Plot", xlab = "X-axis", ylab = "Y-axis", col = "blue", pch = 16)
Graphical parameters in R:
# Using graphical parameters in R x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) plot(x, y, main = "Graphical Parameters", xlab = "X-axis", ylab = "Y-axis", col = "red", pch = 18, lty = 2)