Creating basic plots with ggplot2
In this recipe, we demonstrate how to use The Grammar of Graphics to construct our very first ggplot2
chart with the superstore sales dataset.
Getting ready
First, download the superstore_sales.csv
dataset from the https://github.com/ywchiu/rcookbook/raw/master/chapter7/superstore_sales.csv GitHub link.
Next, you can use the following code to download the CSV file to your working directory:
> download.file('https://github.com/ywchiu/rcookbook/raw/master/chapter7/superstore_sales.csv', 'superstore_sales.csv')
You will also need to load the dplyr
package to manipulate the superstore_sales
dataset.
How to do it…
Please perform the following steps to create a basic chart with ggplot2
:
First, install and load the
ggplot2
package:> install.packages("ggplot2") > library(ggplot2)
Import
superstore_sales.csv
into an R session:> superstore <-read.csv('superstore_sales.csv', header=TRUE) > superstore$Order.Date <- as.Date(superstore$Order.Date) >...