Section 2.7 Ggplot2 Package
You may wonder if we can visualize the information regarding the
RACE variable in the 2012 GSS data in a graph. One of the most popular tools used to create such data visualizations is the ggplot2 package. As with the dplyr, the ggplot2 package is a part of tidyverse. Therefore, you do not need to install or load ggplot2 as long as the tidyverse package is installed and loaded.
The following is a reusable template for making graphs with
ggplot2.
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
To create a graph, substitute the section within the angle brackets in the provided code with either a dataset, a geom function, or a set of mappings.
For instance, let’s create a bar chart to see how the different racial categories are distributed. Bar charts are handy for showing how often each category appears in a variable. Here’s the code using
ggplot2 to make the chart:
ggplot(GSS.2012.cleaned, aes(x = RACE)) +
geom_bar()
This code initializes a ggplot object with our dataset
GSS.2012.cleaned and tells ggplot to use the RACE variable for the x-axis. Then, it adds bars to the plot with geom_bar(). By default, this function counts how many times each category appears in the RACE variable and makes a bar for each one. Now, let’s make the chart a bit more informative by adding labels:
ggplot(GSS.2012.cleaned, aes(x = RACE)) +
geom_bar() +
labs(x = "Race", y = "Frequency", title = "Distribution of Race")
Here,
labs() sets the labels for the x-axis (“Race”), y-axis (“Frequency”), and the plot title (“Distribution of Race”), which gives more context to the chart. With the + operator, we can add more aesthetic options to the bar graph. You can achieve the same result using the pipe operator:
GSS.2012.cleaned %>%
ggplot(aes(x = RACE)) +
geom_bar() +
labs(x = "Race", y = "Frequency", title = "Distribution of Race")
Using the pipe operator can give you additional flexibility when more complex tasks need to be added. For example, we can create a new store place using the pipe operator and ggplot.
race.bar <- GSS.2012.cleaned %>%
ggplot(aes(x = RACE, fill = RACE)) +
geom_bar()
race.bar
race.bar stores the resulting bar chart plot object, allowing you to further customize or display the plot as needed.
Now, I want to add the colors to the bars in the chart. The bars will be filled with “beige”, “black”, and “gray” colors, respectively, corresponding to the different levels of the
RACE variable.
race.bar <- GSS.2012.cleaned %>%
ggplot(aes(x = RACE, fill = RACE)) +
geom_bar() +
scale_fill_manual(values = c("beige", "black", "gray"),
guide = FALSE) +
labs(x = "Race", y = "Frequency", title = "Distribution of Race")
race.bar
scale_fill_manual(values = c("beige", "black", "gray")) sets the fill colors of the bars manually. The values argument specifies a vector of colors to use for filling the bars.
guide = FALSE specifies whether to include a legend or guide for the fill scale. Setting it to FALSE removes the legend, so the colors will be applied directly to the bars without a corresponding legend in the plot.
Finally, you may want to remove the gray background that makes the graph look less professional. You can remove background grids, axis lines, and other non-essential elements, resulting in a clean and simple appearance in ggplot.
race.bar <- GSS.2012.cleaned %>%
ggplot(aes(x = RACE, fill = RACE)) +
geom_bar() +
scale_fill_manual(values = c("beige", "black", "grey"),
guide = FALSE) +
labs(x = "Race", y = "Frequency", title = "Distribution of Race") +
theme_minimal()
race.bar
theme_minimal() applies the minimalistic theme to the plot created by ggplot2, resulting in a plot with a clean and uncluttered appearance.
In this chapter, I introduced several useful data transformation and visualization packages. Additionally, we reviewed some basic codes and functions that can help you recode the variables and visualize data. In the next chapter, we will learn how to produce descriptive statistics.
