1. Learn
  2. /
  3. Courses
  4. /
  5. Helsinki Open Data Science

Connected

Exercise

Visualizations with ggplot2

ggplot2 is a popular library for creating stunning graphics with R. It has some advantages over the basic plotting system in R, mainly consistent use of function arguments and flexible plot alteration. ggplot2 is an implementation of Leland Wilkinson's Grammar of Graphics — a general scheme for data visualization.

In ggplot2, plots may be created via the convenience function qplot() where arguments and defaults are meant to be similar to base R's plot() function. More complex plotting capacity is available via ggplot(), which exposes the user to more explicit elements of the grammar. (from wikipedia)

RStudio has a cheatsheet for data visualization with ggplot2.

Instructions

100 XP
  • Access the ggplot2 library
  • Initialize the plot with data and aesthetic mappings
  • Adjust the plot initialization: Add an aesthetic element to the plot by defining col = gender inside aes().
  • Define the visualization type (points)
  • Draw the plot to see how it looks at this point
  • Add a regression line to the plot
  • Add the title "Student's attitude versus exam points" with ggtitle("<insert title here>") to the plot with regression line
  • Draw the plot again to see the changes