Base R Plotting
Introduction
R provides powerful base plotting functions like plot()
, hist()
, and boxplot()
to create various visualizations. This tutorial demonstrates their usage with examples.
1. Using the plot() Function
The plot()
function is used to create scatter plots, line graphs, and other basic plots.
Example:
# Create sample data x <- 1:10 y <- x^2 # Create a scatter plot plot(x, y, main = "Scatter Plot", xlab = "X-axis", ylab = "Y-axis", col = "blue", pch = 16)
2. Using the hist() Function
The hist()
function is used to create histograms, which visualize the distribution of a dataset.
Example:
# Create a sample dataset data <- c(10, 20, 20, 30, 30, 30, 40, 40, 50, 50, 50, 60) # Create a histogram hist(data, main = "Histogram of Data", xlab = "Values", col = "green", border = "black")
3. Using the boxplot() Function
The boxplot()
function is used to create box-and-whisker plots, which summarize the distribution of a dataset.
Example:
# Create a sample dataset data <- list( Group1 = c(10, 20, 30, 40, 50), Group2 = c(15, 25, 35, 45, 55), Group3 = c(5, 15, 25, 35, 45) ) # Create a boxplot boxplot(data, main = "Boxplot of Groups", xlab = "Groups", ylab = "Values", col = c("red", "blue", "yellow"))
Conclusion
Base R plotting functions provide an easy and effective way to visualize data. The plot()
, hist()
, and boxplot()
functions are versatile tools for creating scatter plots, histograms, and boxplots respectively.