{ "cells": [ { "cell_type": "markdown", "id": "567435ec", "metadata": {}, "source": [ "This tutorial is dedicated to learning how visualize data using the library **ggplot2**. \n", "\n", "We have already seen several built-in functions in R for making plots, but ggplot2 provides a more elegant and versatile way of doing this. It implements a grammar of graphics, i.e. a coherent system for describing and building graphs." ] }, { "cell_type": "markdown", "id": "57bb4508", "metadata": {}, "source": [ "# Loading the library ggplot2" ] }, { "cell_type": "markdown", "id": "5e07d2a7", "metadata": {}, "source": [ "ggplot2 is part of **tidyverse**, a collection of core packages that have become the standard for modelling, transforming and visualizing data in R. \n", "\n", "We'll learn a little bit more about this in the following tutorial, but for now let's just load the package, in the same way as we learned before, that is, using the function `library`." ] }, { "cell_type": "code", "execution_count": 2, "id": "5d7996d0", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Registered S3 methods overwritten by 'tibble':\n", " method from \n", " format.tbl pillar\n", " print.tbl pillar\n", "\n", "Registered S3 methods overwritten by 'ggplot2':\n", " method from \n", " [.quosures rlang\n", " c.quosures rlang\n", " print.quosures rlang\n", "\n", "Registered S3 method overwritten by 'rvest':\n", " method from\n", " read_xml.response xml2\n", "\n", "── \u001b[1mAttaching packages\u001b[22m ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──\n", "\n", "\u001b[32m✔\u001b[39m \u001b[34mggplot2\u001b[39m 3.1.1 \u001b[32m✔\u001b[39m \u001b[34mpurrr \u001b[39m 0.3.2 \n", "\u001b[32m✔\u001b[39m \u001b[34mtibble \u001b[39m 2.1.1 \u001b[32m✔\u001b[39m \u001b[34mdplyr \u001b[39m 0.8.0.\u001b[31m1\u001b[39m\n", "\u001b[32m✔\u001b[39m \u001b[34mtidyr \u001b[39m 0.8.3 \u001b[32m✔\u001b[39m \u001b[34mstringr\u001b[39m 1.4.0 \n", "\u001b[32m✔\u001b[39m \u001b[34mreadr \u001b[39m 1.3.1 \u001b[32m✔\u001b[39m \u001b[34mforcats\u001b[39m 0.4.0 \n", "\n", "── \u001b[1mConflicts\u001b[22m ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──\n", "\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mfilter()\u001b[39m masks \u001b[34mstats\u001b[39m::filter()\n", "\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mlag()\u001b[39m masks \u001b[34mstats\u001b[39m::lag()\n", "\n" ] } ], "source": [ "library(tidyverse)" ] }, { "cell_type": "markdown", "id": "4f6ea550", "metadata": {}, "source": [ "# First steps\n", "\n", "In order to learn practicing, let's load the following dataset:" ] }, { "cell_type": "code", "execution_count": 3, "id": "ee0a3425", "metadata": {}, "outputs": [], "source": [ "sat.dat<-read.csv(\"https://vincentarelbundock.github.io/Rdatasets/csv/psych/sat.act.csv\")" ] }, { "cell_type": "markdown", "id": "d7808356", "metadata": {}, "source": [ "These data correspond to the self reported scores on the SAT Verbal, SAT Quantitative and ACT, and were collected as part of the Synthetic Aperture Personality Assessment (SAPA) web based personality assessment project. Age, gender, and education are also reported." ] }, { "cell_type": "code", "execution_count": 4, "id": "617ace31", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
X | gender | education | age | ACT | SATV | SATQ |
---|---|---|---|---|---|---|
29442 | 2 | 3 | 19 | 24 | 500 | 500 |
29457 | 2 | 3 | 23 | 35 | 600 | 500 |
29498 | 2 | 3 | 20 | 21 | 480 | 470 |
29503 | 1 | 4 | 27 | 26 | 550 | 520 |
29504 | 1 | 2 | 33 | 31 | 600 | 550 |
29518 | 1 | 5 | 26 | 28 | 640 | 640 |