General rule of thumb, if the range is large and large numbers skew results, use log scale. You can either use mutate and generate new column or you can use scaling functions. (Please type scale_ and then press Tab). Below, scale_x_log10() was used for scaling gdpPercap data.
Multiple geom_point layers
ggplot is very powerful and flexible. You can combine different geoms and generate unique plots. Let’s color and emphasize countries which have population greater than 100 million.
We’ll generate another data frame keeping those countries and then plot as a separate geom_point layer.
You can customize almost all aspects of your plot with theme() function. There are theme_* functions which can modify the appearance of the plot by altering several aspects at the same time.
Let’s apply theme_classic() to our plot (result in next slide).
There are many theme packages which can be used to apply themes of popular resources. Let’s apply The Economist theme to your plot with single line of code
labs() function can be used to add title, subtitle and change X and Y axis labels. Below, we added the following layer
labs(x="GDP Per Capita (USD, Log10)", y="Life Expectancy (years)", title="Wealth vs. Life Expectancy", subtitle="Source: Gapminder (range: 1952-2007)")
Interactive HTML output
There are many packages which can embed html compatable plots/tables to your output. The code below generates an interactive plot by plotly package. See the next slide for results.
Plots as objects also help compositing figures from independent figures. Please check patchwork and cowplot packages.
Saving figures
ggsave() function is used to save the plots you generated. It can save plots in PNG, PDF, SVG formats. The resolution and size of the image can be adjusted for high quality/resolution images.
Please check the comprehensive list of ggplot related resources at awesome-ggplot2 repo at Github.
R Graph Gallery has nicely categorized possible plots which might give an idea about what is available.
Ggplot is much more powerful than you can ever imagine. Please browse the galleries by the following users. For some users you might need to browse the individual folders.
Cédric Scherer makes professional plots with ggplot. Check out his gallery at https://github.com/z3tt/TidyTuesday (visit individual folders for R code)