Get startedGet started for free

Make the perfect plot using Shiny

1. Make the perfect plot using Shiny

Imagine you're making a figure for a manuscript using R.

2. Re-plotting using R code

You spend a lot of time re-creating the same plot over and over again by re-running the same code but making small changes in the parameters each time.

3. Re-plotting using R code

The size of the points,

4. Re-plotting using R code

the color of the points,

5. Re-plotting using R code

the plot title,

6. Re-plotting using R code

the data shown on the plot—they all have to be just right before publishing your plot!

7. Re-plotting using Shiny

To save you from the hassle of re-running almost the exact same code many times, in this chapter you'll learn how to create a Shiny app to help make a customizable plot. Using an interactive user interface makes it quicker and easier to experiment with all the plot parameters.

8. Re-plotting using Shiny

This chapter focuses

9. Re-plotting using Shiny

on introducing

10. Re-plotting using Shiny

many different

11. Re-plotting using Shiny

types of Shiny inputs, which you'll use to adjust the plot parameters.

12. Gapminder dataset

Let's begin by introducing the gapminder dataset. Gapminder contains a few basic socioeconomic data for 142 countries between the years 1952 and 2007.

13. Gapminder dataset

Each row in the dataset records

14. Gapminder dataset

the life expectancy,

15. Gapminder dataset

population, and

16. Gapminder dataset

GDP per capita of one country in a given year.

17. Gapminder dataset

The countries are divided into 5 continents and data is recorded in 5-year intervals. This makes gapminder a great dataset for comparing socioeconomic metrics between different countries, continents, or across time.

18. Gapminder package

The gapminder data is available in R from the gapminder package, so we need to run library gapminder before we can start using it. Let's review a few base R commands that will be useful for exploring this dataset. If we want to find the smallest population recorded in the data, we use the min function. Similarly, to find the largest population, we use the max function.

19. Gapminder package

In order to select only a subset of the rows based on some conditions, we use the subset function. For example, to only view observations from Canada before year 1965, we can use this code. Lastly, remember that you can use the dollar sign notation to extract the value of a variable out of the data, even after you subset it. For example, to get Canada's life expectancy in 1962 you can use this code to see the answer, which is 71-point-3.

20. Let's practice!

Before we start building shiny apps, let's first make sure you're familiar with the gapminder data that we'll be using.