Advanced data visualization with R and ggplot2



This practical follows the previous basic introduction to ggplot2. It allows to go further with ggplot2: annotation, theme customization, color palette, output formats, scales, and more.

Get ready


The following libraries are needed all along the practical. Install them with install.packages() if you do not have them already. Then load them with library().

1- General appearance


→ Titles


Q1.1 The code below builds a basic histogram for Rbnb apartment prices on the French Riviera. It shows only value under 300 euros. Add code to:

  • add a title with ggtitle()
  • change axis labels xlab() and ylab()
  • change axis limits with xlim() and ylim()

→ Chart components

All ggplot2 chart components can be changed using the theme() function. You can see a complete list of components in the official documentation.

Note: components are changed using different functions: element_text(), element_line() for lines and so on..




Q1.2 Reproduce the previous histogram and change:

  • plot title size and color with plot.title
  • X axis title size and color with axis.title.x
  • Grid appearance with panel.grid.major