Learn to Create a Radar Chart in R with Data from Our World in Data (2018)
Total Page:16
File Type:pdf, Size:1020Kb
Learn to Create a Radar Chart in R With Data From Our World in Data (2018) © 2021 SAGE Publications, Ltd. All Rights Reserved. This PDF has been generated from SAGE Research Methods Datasets. SAGE SAGE Research Methods: Data 2021 SAGE Publications, Ltd. All Rights Reserved. Visualization Learn to Create a Radar Chart in R With Data From Our World in Data (2018) How-to Guide for R Introduction In this guide, you will learn how to create a radar chart using the R statistical software. Readers are provided links to the example dataset and encouraged to replicate this example. An additional practice example is suggested at the end of this guide. The example assumes you have downloaded the relevant data files to a folder on your computer and that you are using the R statistical software. The relevant code should, however, work in other environments too. Contents 1. Radar Chart 2. An Example in R: Energy Consumption by Type in the Baltics (2018) 2.1 The R Procedure 2.1.1 Preparing the Data 2.1.2 Single Radar Chart 2.1.3 Multiple Radar Charts 2.1.4 Further Edits 2.2 Exploring the Output 3. Your Turn Page 2 of 17 Learn to Create a Radar Chart in R With Data From Our World in Data (2018) SAGE SAGE Research Methods: Data 2021 SAGE Publications, Ltd. All Rights Reserved. Visualization 1. Radar Chart The radar chart maps three or more different quantitative variables across a series of categories at equal intervals along a circle. The quantity in each category is marked by a marker plotted a certain distance away from the central axis, which is then connected sequentially to the other category value markers to create a unique shape, which can be either filled or unfilled. The angles between the points only represent qualitative distinctions, and the overall shape and surface area depend largely on the arrangement of the categories—therefore not actually encoding total value across categories, though it may at first glance seem to do so. Only the distance of each marker from its origin encodes value, and comparisons between categories are similarly based solely on this distance. Radar charts can be compared side-by-side provided that the axes’ maximum and minimum values are identical across the board. Sometimes the radar chart type is also called a spider or web chart due to its resemblance to a spider’s web. The term radar chart is often used interchangeably and confused with the polar chart, which for our purposes is an altogether different chart type, showing quantitative variables using a line drawn on a polar coordinate system. One variable (typically time) is mapped to the angle (theta, Θ), and the other variable to the radius (r). 2. An Example in R: Energy Consumption by Type in the Baltics (2018) Figure 1 shows a radar chart of energy consumption by type across the three Baltic states in 2018, using data from Our World in Data. Each country’s consumption totals are labeled under the country name, as the radar chart presents this in an otherwise misleading way through the shapes and surface areas created from individual marker points—for example, Lithuania at first seems to have consumed the most, when in fact, this distinction belongs to Estonia. The radar chart nevertheless gives a relatively easy at-a-glance impression of which Page 3 of 17 Learn to Create a Radar Chart in R With Data From Our World in Data (2018) SAGE SAGE Research Methods: Data 2021 SAGE Publications, Ltd. All Rights Reserved. Visualization types of energy sources each country consumes, as well as relative amounts within each category. A uniform fill color was used for all countries, as the color in itself does not encode any qualitative or quantitative information. The charts show data for three countries. The data from the chart, in the decreasing order of the source quantity, are as follows. Estonia: 77 terawatt-hours • Coal • Oil • Natural gas Latvia: 37 terawatt-hours • Oil • Natural gas • Hydropower Lithuania: 64 terawatt-hours • Oil • Natural gas • Coal Text under the charts reads, “Source: Our World in Data, 2018.” Figure 1. A Radar Chart of Energy Consumption in the Baltics Page 4 of 17 Learn to Create a Radar Chart in R With Data From Our World in Data (2018) SAGE SAGE Research Methods: Data 2021 SAGE Publications, Ltd. All Rights Reserved. Visualization 2.1 The R Procedure R is a free open-source software and computing platform for statistical analysis with many charting options. R is not based on a graphical interface with pull-down menus. Rather, you input lines of code that execute functions and operations built into R or different packages. It is best to save your code in a simple text file that R users generally refer to as a script file. We provide a script file with this example that executes all of the operations described here. If you are not familiar with R, we suggest you start with the introduction manual located here (http://cran.R- project.org/doc/manuals/R-release/R-intro.html). For this example, we are using RStudio, a free, open-source user interface for R which makes working with R programming easier. In this example, we write our code using R Script, found in the top left of the four windows in R Studio. This means that all actions can be recorded and kept for further use. It is helpful to do this to be able to trace back your steps and decisions made in the analysis. To run the code, you can either press Ctrl + Enter (or Command + Enter on a Mac) after each line of code or highlight the line(s) of code you wish to perform and click Run. (Code can also be written in the Console area in the bottom left, pressing Enter at the end of each line of code. This does not record your actions, however.) Page 5 of 17 Learn to Create a Radar Chart in R With Data From Our World in Data (2018) SAGE SAGE Research Methods: Data 2021 SAGE Publications, Ltd. All Rights Reserved. Visualization Creating the radar chart requires installing some packages and importing the libraries. Install them if you do not have them already, either using the interface or by typing install.packages("packagename") in the console or using the menu item Install packages… under Tools. You will get an error message if something is missing when trying to run the script. If needed, just install the missing packages, and everything should work after that. The necessary packages are: • Tidyverse: a set of packages including readr, dplyr, ggplot2, and many more; a versatile set of tools for handling data and visualizing many graph types in R, https://cran.r-project.org/web/packages/tidyverse/readme/ README.html • fmsb: a package primarily intended for medical and health data analysis, but also includes the radar chart methods, https://CRAN.R-project.org/ package=fmsb • colr: a convenient package for renaming data frame columns, https://CRAN.R-project.org/package=colr Install these as needed and save the tutorial csv data file primary-energy- consumption-by-source.csv to a folder on your computer. The example uses a folder called sage_r-sourcedata in the user root, where the table goes in subfolder tables. If you choose to save your files elsewhere, just update the path accordingly. Begin by running the code in the script file up to line four by marking the lines and hitting control + enter (command + enter on a mac), this will import the necessary libraries. 2.1.1 Preparing the Data We will begin by importing our dataset: Page 6 of 17 Learn to Create a Radar Chart in R With Data From Our World in Data (2018) SAGE SAGE Research Methods: Data 2021 SAGE Publications, Ltd. All Rights Reserved. Visualization consumption <- read_delim("~/sage_r-sourcedata/tables/primary- energy-consumption-by-source.csv", ",") You can take a look at the data table by typing View(consumption) into your Console panel in the bottom left quadrant of the interface or by opening the data table from the top right Environment panel. You can also view just the column names with the command colnames(consumption). We will keep this data table in reserve and make subsets of it for each of our different plot versions (Figure 2). The data are tabulated below. Oil (terawatt- Natural Gas Coal (terawatt- Nuclear (terawatt- Hydropower Entity Code Year hours) (terawatt-hours) hours) hours) (terawatt-hours) Africa NA 1965 328.24966 9.543754 335.580402 0.0000000 14.2788056 Africa NA 1966 359.14315 10.669916 331.361013 0.0000000 15.6490489 Africa NA 1967 356.36648 10.545670 341.000471 0.0000000 16.1583330 Africa NA 1968 376.00921 10.688970 355.393857 0.0000000 18.6229828 Africa NA 1969 381.17900 12.492000 357.494212 0.0000000 21.5828968 Africa NA 1970 421.12586 15.520325 367.958023 0.0000000 27.0762870 Africa NA 1971 475.80862 18.405264 388.717309 0.0000000 25.8366053 Africa NA 1972 515.50459 24.670657 392.431563 0.0000000 29.83868784 Africa NA 1973 554.28050 39.551791 414.901083 0.0000000 29.8037420 Africa NA 1974 572.99627 44.518487 432.131850 0.0000000 35.0891855 Africa NA 1975 599.15275 53.638377 549.067310 0.0000000 36.8790484 Page 7 of 17 Learn to Create a Radar Chart in R With Data From Our World in Data (2018) SAGE SAGE Research Methods: Data 2021 SAGE Publications, Ltd. All Rights Reserved. Visualization Africa NA 1976 665.14780 60.981390 481.936906 0.0000000 40.8611792 Africa NA 1977 703.88885 68.234191 492.911858 0.0000000 45.0833511 Africa NA 1978 740.60696 97.369898 477.924213 0.0000000 46.0850793 Africa NA 1979 789.47579 148.842816 504.547560 0.0000000 47.2802068 Africa NA 1980 835.99012 186.913977 543.879963 0.0000000 46.0301706 Africa NA 1981 889.82084 228.491156 636.189973 0.0000000 48.3774465 Africa NA 1982 932.52345 246.733549 702.560899 0.0000000 48.6327147 Africa NA 1983 964.19883 271.275347 710.034781 0.0000000 45.5089147 Africa NA 1984 984.73530 259.902780 762.60588 3.9250000 45.1259603 Africa NA 1985 1006.97520 278.842747 787.165889 5.3150000 49.4467507 Figure 2.