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 in 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.

1. Basic Scatter Plot

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")

2. Improve Aesthetics

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.

3. Add Regression Line

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

4. Scatter Plot with ggplot2

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!

  1. Creating scatter plots in R:

    • Scatter plots visualize the relationship between two continuous variables.
    # 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")
    
  2. Scatter plot examples in R:

    • Explore various scenarios and datasets for creating scatter plots.
    # 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")
    
  3. Customizing scatter plots in ggplot2:

    • Use the 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")
    
  4. Adding color to scatter plots in R:

    • Enhance scatter plots by adding color to data points.
    # Example of a scatter plot with color
    plot(x, y, col = "red", main = "Scatter Plot with Color")
    
  5. Scatter plot with regression line in R:

    • Include a regression line to visualize trends in the data.
    # Example of a scatter plot with regression line
    plot(x, y, main = "Scatter Plot with Regression Line")
    abline(lm(y ~ x), col = "blue")
    
  6. Grouped scatter plots in R:

    • Group data points in scatter plots for better comparisons.
    # Example of grouped scatter plots
    group <- rep(c("A", "B"), each = 3)
    plot(x, y, col = group, main = "Grouped Scatter Plot")
    
  7. Interactive scatter plots in R:

    • Use interactive packages like 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"))
    
  8. Scatter plot matrix in R:

    • Visualize relationships between multiple variables with a scatter plot matrix.
    # Example of a scatter plot matrix
    pairs(random_data)
    
  9. Scatter plot labels in R:

    • Add labels to data points for better interpretation.
    # Example of a scatter plot with labels
    text(x, y, labels = c("A", "B", "C", "D", "E"), pos = 3)
    
  10. Bubble charts in R:

    • Use bubble charts to represent an additional dimension with bubble size.
    # Example of a bubble chart
    size <- c(10, 20, 30, 40, 50)
    symbols(x, y, circles = size, inches = 0.2, add = TRUE)
    
  11. Scatter plot size and shape customization in R:

    • Customize the size and shape of data points in scatter plots.
    # Example of scatter plot size and shape customization
    plot(x, y, pch = 16, cex = 2, main = "Customized Scatter Plot")
    
  12. Faceted scatter plots in R:

    • Create faceted scatter plots for comparing subsets of the data.
    # 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)
    
  13. 3D scatter plots in R:

    • Visualize data in three dimensions using 3D scatter plots.
    # Example of a 3D scatter plot
    library(scatterplot3d)
    scatterplot3d(x, y, rnorm(5), main = "3D Scatter Plot")
    
  14. **Saving and exporting