Hello! Welcome to ggplot2 made simple. By far, ggplot2 is one of the most powerful tool in R to create graph and visualize data. Before we throw code at you, lets understand the basic structure of ggplot2.
The base batter/dough of a few main ingredient can be made into different desert through different technique of baking. You cook the batter in a pan you get pancake, in an oven you get a cake, and deep fried give you corn dog!
The main ‘batter’ function where you will feed your data is “ggplot()”. You will able to specify what data frame you are using to build the graph, which vector in your data frame is your x and y axis, and even which color to distinguish your categorical variable. The cooking process is done with the variety of “geom_” function. Using geom_bar() on your ggplot() will yield a bar graph, geom_point() will yield a scatter plot, and geom_boxplot() will make a box plot! Within each geom_ function, there are more parameters that will allow you to customize further. Pretty cool and simple eh?
Further customization can be added on with even more function! Simply do + function() and it will add on to the existing graph!
Lets build a quick box plot to demonstrate. First we need to get data! Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
This is your iris data frame! Notice how it is very similar to an excel sheet. Each column are vectors. Each vector name are present at the very top i.e: Sepal.Length, Sepal.Width Petal.Length, Petal.Width, Species. Each rows show the element of each vector at a numbered position, ie: Row 6 contain 5.4, 3.9, 1.7, 0.4, setosa for each respective aforementioned vector. For now, only worry about how column vector create the basic structure of your data frame since the vectors is what you will use to create graphs.
library(ggplot2) # load in the ggplot2 library to be able to access all the ggplot2 function, use install.packages(ggplot2) if not installed. Then load the library
ggplot(iris, aes(x = Species,y = Sepal.Width, fill = Species)) + # Your batter
geom_boxplot() # Your cooking methodSee how the original ggplot is “cooked” with geom_boxplot() in the code? This is the essence of creating graph in ggplot and R in general! We will dive deeper into the code later but for now, let add frosting and other goods to our cake!
library(viridisLite) #load in colorblind friendly color
ggplot(iris, aes(x = Species,y = Sepal.Width, fill = Species)) + # Your batter
geom_boxplot() + # Your cooking method
xlab("Species of Iris") + # X-axis title
ylab("Sepal Width") + # Y-axis title
scale_x_discrete(labels = c('Setosa', 'Versicolor', 'Virginica')) + # change x axis element label
scale_fill_manual(values = viridis(3)) + #Make the color colorblind friendly!
theme_bw() + #change the general theme look of the graph!
theme(legend.position="none") + #remove legend
geom_jitter() #add jitterLook how beautiful this graph is compared to the original base graph! It is clear, precise, and colorblind friendly! More modification of the base ggplot graph can be achieve by adding on more and more function with “+”. This is the equivalent to frosting and decorating your cake! Now that we understand the basic structures, let make the boxplot step by step! Proceed to the Box plot tab at the top.
This section will look at what is needed in ggplot() to make the box plot and what are the customization within the boxplot() function. Addationally, we will also look at a few customization function relevant to boxplot. Other customization such as color, text, and axis will be tackle in their own seperate section.
The box plot is probably one of the most common plot you will find in science paper, presentation, and grade book! It is very useful because it visualize numerical distribution between different categorical groups. And that directly tie to our first step! We must have discrete categorical variable for our X-axis and continuous numerical variable for our Y-axis. Let’s take a look at our Iris data set for our x and y axis!
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
Within the Iris data set, Species is the only vector that contain categorical elements. Everything else is numerical from Sepal length, width, and petal length and width. Lets see how many unique categorical element in Species.
setosa versicolor virginica
50 50 50
We can see that there are three unique elements within the Species vector: setosa, versicolor, and virginica. Each elements appear 50 times as indicated by the 50 beneath them. In summary, the Species vector of the Iris data frame contain 150 elements with only three unique elements. Even though we already seen the graph from the Introduction section, we can predict that for the X-axis of our box plot, we will have three separate X-axis ticks representing the three unique element.