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 in 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. Base Plotting in R:

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

2. ggplot2: Advanced Plotting:

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)

Summary:

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.

  1. Graph plotting in R:

    • Description: R provides several packages for creating and customizing plots. The most common ones include base graphics (plot()), ggplot2, lattice, and others.
    • Code:
      # 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")
      
  2. Creating graphs in R:

    • Description: Graphs in R can be created using various functions and packages. The choice depends on the type of graph and customization requirements.
    • Code:
      # 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")
      
  3. R base graphics:

    • Description: R's base graphics system provides functions like plot(), barplot(), and lines() for creating basic plots without additional packages.
    • Code:
      # 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")
      
  4. Scatter plot in R:

    • Description: Scatter plots are useful for visualizing the relationship between two continuous variables.
    • Code:
      # 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")
      
  5. Bar chart in R:

    • Description: Bar charts are effective for visualizing the distribution of categorical data.
    • Code:
      # 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")
      
  6. Line plot in R:

    • Description: Line plots connect data points with lines, useful for showing trends over time.
    • Code:
      # 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")
      
  7. R plot function examples:

    • Description: The plot() function in R is versatile and can be used for various types of plots. Examples include scatter plots, line plots, and more.
    • Code:
      # 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")
      
  8. Customizing plots in R:

    • Description: Plots in R can be customized using various parameters such as colors, labels, and titles.
    • Code:
      # 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)
      
  9. Graphical parameters in R:

    • Description: Graphical parameters in R control aspects like color, line type, and point type in plots.
    • Code:
      # 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)