Set up the options

  • To begin with, make sure that you disable warnings, messages, and errors by setting the global options in the first line chunk knitr::opts_chunk$set.

    • You can activate any option within a code chunk options.
    • You can also save the cache when dealing with large documents that require a lot of memory, but be aware that it may have some problems updating.
  • If you are working in a RMarkdown document and you find annoying to see the output below your code chunk, you can disable it by setting editor_options in your document information and set chunk_output_type: console. The output will appear in your console window next time you will open the document and run the code.

Disclaimer

  • Most of the examples and code of this Html file, especially from the first section, come from chapters 3-6 of Helay (2019).

Let’s create a plot

Ggplot provides you with a set of tools to map data to visual elements on your plot, to specify the kind of plot you want, and then to control the fine details of how it will be displayed.

There is some structured relationship, some mapping, between the variables in your data and their representation in the plot displayed on your screen or on the page.

In this example we will work with data from the gapminder project. Notice that the data is already tidy and ready for analysis. Check out the structure str() and names().

If you are going to experiment with different visual design with the same variables, you can create an object, p, which will contain the core information for our plot.

gapminder <- as_tibble(gapminder)

p <- gapminder %>% 
  ggplot(mapping = aes(x=gdpPercap,
                       y=lifeExp))

p