From the course: Data Visualization in R with ggplot2 (2018)
Introducing ggplot2
From the course: Data Visualization in R with ggplot2 (2018)
Introducing ggplot2
- [Instructor] Ggplot2 is the most popular and fully-featured data visualization package for the R programming language. The power of ggplot2 comes from the fact that it allows you to build and customize graphics in exactly the manner you'd like them to appear using a concept known as the grammar of graphics. If you've ever struggled with creating a visualization in Excel because you couldn't figure out how to tweak the graphic to appear exactly the way you'd like, ggplot2 is for you. Ggplot2 allows you to easily create simple visualizations while also permitting you to define the precise details of a visualization as specifically as you'd like. Ggplot2 is part of a collection of R packages designed for data analysis, known as the tidyverse. Curated by Hadley Wickham, the Chief Scientist at RStudio, the tidyverse packages provide R developers with a set of tools that follow the entire data analysis lifecycle. Let's talk briefly about a few of the components of the tidyverse and how they fit into the data analysis lifecycle. The readr package contains a set of functions designed to import data into R in various forms. In this course, we'll use the read_csv function from the readr package to read data files consisting of comma-separated values, but readr has a lot more available to help you handle tab-separated files, Excel files, and other common data formats. The tibble package defines a new data structure called the tibble that makes it easy to manipulate data in R. The tibble replaces the data frame structure used in Base R, providing a similar data structure that's simpler to work with. The dplyr package contains a set of functions to help you with data manipulation. You'll find functions that select the variables you'd like to include in your analysis, filter the rows included in a tibble, sort data, create new variables, and summarize values using aggregate functions. The tidyr package provides functions that help you create tidy datasets by making wide datasets long with the gather function and making long datasets wide with the spread function. You can learn more about these tidyverse packages in my course, Data Wrangling in R, but what we're most concerned about in this course is the ggplot2 package. Ggplot2 is a set of functions that implement the grammar of graphics and allow you to visualize your data using scatterplots, bar and column graphs, lines, and basically another other type of visualization that you can imagine. Ggplot2 is especially powerful because it is integrated with all of the other components of the tidyverse. You can read data in using the readr package, manipulate it with dplyr and tidyr, and then visualize it with ggplot2. All of your data passes easily between packages because they're designed to be compatible. You'll see that when we get into some examples later in this course. It's easy to install ggplot2. While you can install this package alone, the easiest way to install it is to simply install the entire tidyverse all at once. You can do that by issuing a single command in the R console. Just type install.packages, and then insides of double quotes, put the word tidyverse. When you hit Enter, R will go ahead and begin the installation process. It tells you a lot about all the various packages and dependencies that it needs to load to get the tidyverse set up. This will take a few minutes depending upon what packages are already installed on your computer and the current version of those packages. Once this finishes, you now have the entire tidyverse installed on your system and ready to use. When you're inside of an R script, you can simply issue the command library(tidyverse) to go ahead and load the tidyverse into memory and make it accessible to your code. When you load the tidyverse, you will see some error messages about conflicts between packages in the tidyverse and packages that are already installed on your system. This is normal and it's okay. Now that you've installed the tidyverse, you're ready to begin creating some visualizations.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.