Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Creating Plots in R

Introduction to Plotting in R

Plotting is a fundamental aspect of data visualization, allowing us to represent data graphically. R provides a rich ecosystem for creating various types of plots to understand data better and communicate insights effectively. In this tutorial, we will cover the basics of creating plots using R.

Setting Up Your Environment

Before we dive into plotting, ensure that you have R installed on your machine. You can download it from the CRAN website. Additionally, it is recommended to use RStudio, an integrated development environment (IDE) for R, which makes coding in R easier.

Basic Plotting with Base R

R's base plotting system is simple and intuitive. Let's start with a basic scatter plot.

Example: Creating a simple scatter plot.

R plot(mtcars$wt, mtcars$mpg, main="Scatterplot of Weight vs. MPG", xlab="Weight (1000 lbs)", ylab="Miles per Gallon", pch=19, col="blue")

In this example, we use the built-in mtcars dataset to plot the weight of cars against their miles per gallon (MPG). The plot function takes x and y values along with additional parameters for customization.

Output: A scatter plot displaying the relationship between weight and MPG.

Customizing Your Plots

Customization is key to effective visualization. You can modify various aspects of your plot, including titles, axis labels, colors, and point shapes.

Example: Customizing a scatter plot.

R plot(mtcars$wt, mtcars$mpg, main="Customized Scatterplot", xlab="Weight (1000 lbs)", ylab="Miles per Gallon", pch=19, col="red", cex=1.5)

Here, we have changed the color of the points to red and increased their size using the cex parameter.

Output: A customized scatter plot with red points and larger size.

Creating Different Types of Plots

R supports various plot types. Let's explore a few common types.

Bar Plots

Example: Creating a bar plot.

R barplot(table(mtcars$cyl), main="Bar Plot of Cylinder Counts", xlab="Number of Cylinders", ylab="Frequency", col="lightgreen")

This code generates a bar plot showing the frequency of different cylinder counts in the mtcars dataset.

Output: A bar plot displaying the count of cars for each cylinder type.

Histograms

Example: Creating a histogram.

R hist(mtcars$mpg, main="Histogram of Miles per Gallon", xlab="Miles per Gallon", col="lightblue", border="black")

The histogram above illustrates the distribution of MPG values in the mtcars dataset.

Output: A histogram showing the distribution of miles per gallon.

Advanced Plotting with ggplot2

For more advanced and aesthetically pleasing plots, the ggplot2 package is widely used. To get started, you need to install and load the package.

Example: Installing and loading ggplot2.

R install.packages("ggplot2") library(ggplot2)

After loading the package, you can create a scatter plot using ggplot2 with enhanced flexibility.

Example: Creating a scatter plot with ggplot2.

R ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(color="blue", size=3) + labs(title="Scatterplot of Weight vs. MPG", x="Weight (1000 lbs)", y="Miles per Gallon")

This ggplot2 code generates a scatter plot with customized aesthetics, including color and size of points.

Output: A scatter plot created using ggplot2 with customized aesthetics.

Conclusion

In this tutorial, we covered the basics of creating plots in R using both base R and the ggplot2 package. We explored various types of plots, customization options, and advanced features. Effective data visualization is essential in data analysis, and mastering these skills will greatly enhance your ability to communicate insights from your data.