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
Scatter plots are fundamental in data visualization for depicting the relationship between two continuous variables. In R, the primary function used for creating scatter plots is plot()
. Let's walk through a tutorial on creating scatter plots in R.
First, ensure you have R and RStudio installed. Launch RStudio and proceed as follows.
# Sample data x <- c(1, 2, 3, 4, 5) y <- c(1, 4, 9, 16, 25) # Basic scatter plot plot(x, y, main="Basic Scatter Plot", xlab="X Axis", ylab="Y Axis")
You can enhance the appearance using various parameters.
# Enhanced scatter plot plot(x, y, main="Enhanced Scatter Plot", xlab="X Axis", ylab="Y Axis", col="blue", pch=19, cex=1.5, lwd=3)
Here:
col
: Color of the points.pch
: Point character.cex
: Point size.lwd
: Line width.If you want to add a regression line to your scatter plot:
# Scatter plot with regression line plot(x, y, main="Scatter Plot with Regression Line", xlab="X Axis", ylab="Y Axis", col="blue", pch=19, cex=1.5, lwd=3) abline(lm(y~x), col="red") # Regression line
ggplot2
is a popular package for advanced data visualization. If you haven't installed it yet, you can do so with install.packages("ggplot2")
.
# Load ggplot2 library(ggplot2) # Data in data frame format df <- data.frame(x, y) # Scatter plot with ggplot2 ggplot(df, aes(x=x, y=y)) + geom_point(aes(color=x), size=3) + # size and color can be controlled here ggtitle("Scatter Plot with ggplot2") + xlab("X Axis") + ylab("Y Axis") + geom_smooth(method="lm", se=FALSE, color="red") # Adding regression line
This code will generate a scatter plot using ggplot2, with points colored based on their x-values and a regression line.
With ggplot2
, you can easily layer multiple visualization elements and customize their appearance to a great extent.
This should get you started with creating scatter plots in R. Remember that R's plotting capabilities are extensive and you can customize your plots in many ways. Explore the documentation and various online resources to dive deeper!
Creating scatter plots in R:
# Example of creating a basic scatter plot x <- c(1, 2, 3, 4, 5) y <- c(3, 5, 7, 9, 11) plot(x, y, main = "Scatter Plot Example", xlab = "X-axis", ylab = "Y-axis")
Scatter plot examples in R:
# Example of a scatter plot with random data set.seed(123) random_data <- data.frame(x = rnorm(100), y = rnorm(100)) plot(random_data$x, random_data$y, main = "Random Scatter Plot")
Customizing scatter plots in ggplot2:
ggplot2
package for creating and customizing scatter plots.# Example of a ggplot2 scatter plot with customization library(ggplot2) ggplot(random_data, aes(x = x, y = y)) + geom_point(color = "blue") + labs(title = "Customized Scatter Plot", x = "X-axis", y = "Y-axis")
Adding color to scatter plots in R:
# Example of a scatter plot with color plot(x, y, col = "red", main = "Scatter Plot with Color")
Scatter plot with regression line in R:
# Example of a scatter plot with regression line plot(x, y, main = "Scatter Plot with Regression Line") abline(lm(y ~ x), col = "blue")
Grouped scatter plots in R:
# Example of grouped scatter plots group <- rep(c("A", "B"), each = 3) plot(x, y, col = group, main = "Grouped Scatter Plot")
Interactive scatter plots in R:
plotly
for interactive scatter plots.# Example of an interactive scatter plot with plotly library(plotly) plot_ly(x = x, y = y, mode = "markers", type = "scatter", marker = list(color = "green"))
Scatter plot matrix in R:
# Example of a scatter plot matrix pairs(random_data)
Scatter plot labels in R:
# Example of a scatter plot with labels text(x, y, labels = c("A", "B", "C", "D", "E"), pos = 3)
Bubble charts in R:
# Example of a bubble chart size <- c(10, 20, 30, 40, 50) symbols(x, y, circles = size, inches = 0.2, add = TRUE)
Scatter plot size and shape customization in R:
# Example of scatter plot size and shape customization plot(x, y, pch = 16, cex = 2, main = "Customized Scatter Plot")
Faceted scatter plots in R:
# Example of faceted scatter plots facet_data <- data.frame(x = rnorm(100), y = rnorm(100), group = rep(1:2, each = 50)) ggplot(facet_data, aes(x = x, y = y)) + geom_point() + facet_wrap(~group)
3D scatter plots in R:
# Example of a 3D scatter plot library(scatterplot3d) scatterplot3d(x, y, rnorm(5), main = "3D Scatter Plot")
**Saving and exporting