More Geoms
So far we have been only looking at scatter plots by adding
geom_point
after a ggplot
. In this lesson
let us cover additional plots that can be made with other
geom objects.
Line Plot
In the below code editor we replace the geom_point
we have been
using up till now, with geom_line
. This will change
the graph from a scatter plot to a line plot.
It seems the plot doesn't effectively represent the data using
geom_line
. While, the scatter plot presents the
values of two variables as points on a two-dimensional graph.
In contrast, the line plot presented here
connects the data points with a line. In this case, since the points are flower samples
that don't exhibit a linear relationship, the plot appears
cluttered and lacks clear information. While geom_line
isn't
the optimal choice for visualizing this dataset, it can still be utilized
to depict other aspects, such as the average Petal.Length of the flowers.
Which we will look at next.
To enhance the visualization, we can overlay a line representing the
average Petal.Length on the existing coloured plot. This can be
achieved by appending geom_line
to the
geom_point
code using a +
symbol. Within the
geom_line
function, we set the y-axis argument equal
to the average Petal.Length.
In the geom_line
function we added another aes
.
However, we omitted adding the x-axis argument here
since we want it to inherit the same x-axis in the ggplot object
that geom_point
uses. As such we are in essence overwriting
the y-axis argument in the geom_line
function with this
second aes
call.
Bar Plot
Next, we will explore another useful visualization tool: the bar plot.
The bar plot excellently depict differences in magnitude across
various categories. We will create one using the geom_col
function, where we will look at the total sum Petal.Lengths for
each Species.
To construct this, we'll designate the Species category as our
x-axis, while the total sum of the Petal.Lengths will determine
the height of the bars on the y-axis. We further add a fill to
colour in the boxes. Before embarking on this, we
first need to calculate these total sums. We will do so, by employing a
group_by
function followed by a sum
function to
obtain the necessary data.
Violin plot
The final type of visualization we will explore is known as a violin plot. A violin plot merges the characteristics of a box plot with a density plot, thereby providing a comprehensive view of the distribution of our data.
In this kind of plot, just as in the box plot, the x-axis typically represents different categories, while the y-axis demonstrates the continuous data values corresponding to those categories. Wherever the data is more dense, will create a bulge and create a "violin" shape visualization.
We can make a violin plot with the geom_violin
function. Let's
take a look at it!
There is a vast array of plots available for crafting detailed and insightful visualizations. To explore the extensive selection and find the one that best suits your data storytelling needs, visit the ggplot2 official webpage. Here, you will find a comprehensive reference guide to aid you in creating the most effective and illustrative plots.