{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ggplot2\n", "The ggplot2 is one of the most powerful packages for plotting chars.\n", "Although very powerful, the learning curve for ggplot2 is high, as it is established on a grammar of graphics (https://www.amazon.com/Grammar-Graphics-Statistics-Computing/dp/0387245448) approach." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## graphics.R\n", "\n", "The graphic.R enables plotting charts encapsulating ggplot2.\n", "It enables an easy startup while learning how to use ggplot2. \n", "\n", "The majority of functions require a data.frame with two attributes or more attributes. In most cases, the first attribute is associated with the x-axis. In contrast, the second is related to the y-axis.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Library\n", "The library $myGraphics.R$ is loaded using the source function. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Loading required package: daltoolbox\n", "\n", "Registered S3 method overwritten by 'quantmod':\n", " method from\n", " as.zoo.data.frame zoo \n", "\n", "\n", "Attaching package: ‘daltoolbox’\n", "\n", "\n", "The following object is masked from ‘package:base’:\n", "\n", " transform\n", "\n", "\n" ] } ], "source": [ "# DAL ToolBox\n", "# version 1.1.727\n", "\n", "source(\"https://raw.githubusercontent.com/cefet-rj-dal/daltoolbox/main/jupyter.R\")\n", "\n", "#loading DAL\n", "load_library(\"daltoolbox\") \n", "\n", "# The easiest way to get ggplot2 is to install the whole tidyverse:\n", "# install.packages(\"tidyverse\")\n", "# Alternatively, install just ggplot2:\n", "# install.packages(\"ggplot2\")\n", "#Use suppressPackageStartupMessages(source(filename)) to avoid warning messages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Color palette\n", "\n", "One thing very relevant while plotting charts is to preserve visual identity. \n", "For that, the color brewer is an excellent tool to set up colors for your graphics using appropriate colors.\n", "More information: https://colorbrewer2.org\n", "\n", "Take some time to look at how to use it in R: https://rdrr.io/cran/RColorBrewer/man/ColorBrewer.html." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Loading required package: RColorBrewer\n", "\n" ] } ], "source": [ "load_library(\"RColorBrewer\")\n", "col_set <- brewer.pal(9, 'Set1')\n", "colors <- col_set[1:4]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Loading required package: ggplot2\n", "\n" ] } ], "source": [ "load_library(\"ggplot2\")\n", "# setting the font size for all charts\n", "font <- theme(text = element_text(size=16))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "4.3.3" } }, "nbformat": 4, "nbformat_minor": 4 }