Start by loading the dplyr and ggplot2 library as well as the
murders
data.
library(dplyr)
library(ggplot2)
library(dslabs)
data(murders)
Now we are going to change the x-axis to a log scale to account for the fact the distribution of population is skewed. Let’s start by defining an object p
holding the plot we have made up to now:
p <- murders %>%
ggplot(aes(population, total, label = abb)) +
geom_label(aes(col=region))
Hint
You can add layers to an existing ggplot object with the
+
object. To add an extra layer top
you can write:
p_with_extra_layer <- p + EXTRA_LAYER
To change the x-axis to a log scale we learned about the scale_x_log10()
layer. Add this layer to the object p
defined in the example to change the scale on the X-axis. Store the resulting GGPlot object in p_x_scale
.
Add an extra layer to the object p_x_scale
to change the y-axis to a log scale using the scale_y_log10()
layer. Store the resulting GGPlot object in p_xy_scale
.
Add one more extra layer to the p_xy_scale
object to add the title "US Gun Murders in 2010" to the plot using the ggtitle()
layer. Save the resulting GGPlot object in p_title
.