Make sure to run this code once to load our items
Let’s make a filtered data set that includes only two years, 2002 and 2007. Note how we save it to an object, gapminder_filtered
# Make sure you ran the chunk above to load your libraries
gapminder_filtered <- gapminder %>%
filter(year > 2000)
## Start by making a facted plot
base_plot <- ggplot(data = gapminder_filtered,
mapping = aes(x = gdpPercap, y = lifeExp,
color = continent, size = pop)) +
geom_point() +
facet_wrap(vars(year))
base_plot
Now, fix the x-axis so it is logged gdpPercap. You will need a type of scale()
function. Note that if you paste labels = dollar_format(accuracy = 1)
inside the parentheses of your scale function, you can change how the dollar values are presented!
# base_plot <- base_plot + CODE HERE
base_plot
What does this modification do?
base_plot <- base_plot +
scale_size_continuous(labels = comma)
base_plot
Let’s adjust the color scale. Oceania is really hard to see! What can we do? We can exclude the top-most value of the range by adding end = 0.9
to the scale_color_viridis_d
option. Because it’s a new option, make sure to separate by a comma.
Fix the color scale now:
base_plot <- base_plot +
# Use viridis
scale_color_viridis_d(option = "plasma")
base_plot
Now, let’s add some labels:
Remember the generic form:
labs(x = "XTITLE", y = "YTITLE",
`color = "COLOR LEGEND TITLE", size = "SIZE LEGENDTITLE",
`title = "TITLE",
`subtitle = "SUBTITLE",
caption = "CAPTION)
#base_plot <- base_plot +
# labs(STUFFF)
base_plot
ggThemeAssist
If you’re feeling adventurous, you can mess more with ggThemeAssist:
#install.packages("ggThemeAssist")
library(ggThemeAssist)
base_plot
# First, run this chunk to load the library
# Then, highlight base_plot.
# Then click on "Addins" and select "ggplot Theme Assistant"
# Play!
# When you're done, check out the code you've created.
Finally, let’s export it as a png file!
Remember the generic form :
ggsave(filename = "NAME.png", plot = TITLE, width = 6, height = 4.5, units = "in")
# Export to a png file
Upload your png file to blackboard, and you’re done!