Path: blob/master/2020-fall/materials/tutorial_04/tutorial_04.ipynb
2051 views
Tutorial 4: Effective Data Visualization
Lecture and Tutorial Learning Goals:
Expand your data visualization knowledge and tool set beyond what we have seen and practiced so far. We will move beyond scatter plots and learn other effective ways to visualize data, as well as some general rules of thumb to follow when creating visualizations. All visualization tasks this week will be applied to real world data sets. Remember, it is an iterative process to answer questions and each step taken should have a good reason behind it.
After completing this week's lecture and tutorial work, you will be able to:
Describe when to use the following kinds of visualizations:
scatter plots
line plots
bar plots
histogram plots
Given a dataset and a question, select from the above plot types to create a visualization that best answers the question
Given a visualization and a question, evaluate the effectiveness of the visualization and suggest improvements to better answer the question
Identify rules of thumb for creating effective visualizations
Define the three key aspects of ggplot objects:
aesthetic mappings
geometric objects
scales
Use the
ggplot2
library in R to create and refine the above visualizations using:geometric objects:
geom_point
,geom_line
,geom_histogram
,geom_bar
,geom_vline
,geom_hline
scales:
scale_x_continuous
,scale_y_continuous
aesthetic mappings:
x
,y
,fill
,colour
,shape
labelling:
xlab
,ylab
,labs
font control and legend positioning:
theme
flipping axes:
coord_flip
subplots:
facet_grid
Describe the difference in raster and vector output formats
Use
ggsave
to save visualizations in.png
and.svg
format
Any place you see ...
, you must fill in the function, variable, or data to complete the code. Replace fail()
with your completed code and run the cell!
Question 0.1
{points: 1}
Match the following definitions with the corresponding aesthetic mapping or function used in R:
Definitions
A. Prevents a chart from being stacked. It preserves the vertical position of a plot while adjusting the horizontal position.
B. In bar charts, this aesthetic fills in the bars by a specific colour or separates the counts by a variable different from the x-axis.
C. In bar charts, it outlines the bars but in scatterplots, it fills in the points (colouring them based on a particular variable aside from the x/y-axis).
D. This makes the height of each bar equal to the number of cases in each group, and it is incompatible with mapping values to the y aesthetic. This stat basically allows the y-axis to represent particular values from the data instead of just counts.
E. This aesthetic allows further visualization of data by varying data points by shape (modifying their shape based on a particular variable aside from the x/y-axis).
F. Labels the y-axis.
Aesthetics and Functions
colour
dodge
fill
identity
ylab
shape
For every description, create an object using the letter associated with the definition and assign it to the corresponding number from the list above. For example: B <- 1
Question 0.2 True or False:
{points: 1}
We should save a plot as an .svg
file if we want to be able to rescale it without losing quality.
Assign your answer (either "true" or "false") to a variable named answer0.2
.
1. Data on Personal Medical Costs
As we saw in the worksheet, data scientists work in all types of organizations and with all kinds of problems. One of these types of organizations are companies in the private sector that work with health data. Today we will be looking at data on personal medical costs. There are varying factors that affect health and consequently medical costs. Our goal for today is to determine how are variables related to the medical costs billed by health insurance companies.
To analyze this, we will be looking at a dataset that includes the following columns:
age
: age of primary beneficiarysex
: insurance contractor gender: female, malebmi
: body mass index, providing an understanding of body, weights that are relatively high or low relative to height, objective index of body weight (kg/) using the ratio of height to weight, ideally 18.5 to 24.9children
: number of children covered by health insurance / number of dependentssmoker
: smokingregion
: the beneficiary's residential area in the US: northeast, southeast, southwest, northwest.charges
: individual medical costs billed by health insurance
This dataset, was taken from the collection of Data Sets created and curated for the Machine Learning with R book by Brett Lantz.
Question 1.1 Yes or No:
{points: 1}
Based on the information given in the cell above, do you think the column charges
includes quantitative, numerical data?
Assign your answer (either "yes" or "no") to an object called answer1.1
.
Question 1.2 Multiple Choice:
{points: 1}
Assuming overplotting is not an issue, which plot would be the most effective to compare the relationship of age
and charges
?
A. Scatterplot
B. Stacked Bar Plot
C. Bar Plot
D. Histogram
Assign your answer to an object called answer1.2
.
Question 1.3
{points: 1}
Read the insurance.csv
file in the data/
folder and look at the last 6 individuals presented.
Assign your answer to an object called insurance
.
Question 1.4
{points: 3}
Looking over the loaded data shown above, what observations can you make about medical charges and age? How about medical charges and BMI? Finally, what about medical charges and smoking?
Also, comment on whether our observations might change if we visualize the data? And/or whether visualizing the data might allow us to more easily make observations about the relationships in the data as opposed to trying to make them directly from the data table?
Answer in the cell below.
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.
Question 1.5
{points: 1}
According to the National Heart, Lung and Blood Institute of the US: "The higher your BMI, the higher your risk for certain diseases such as heart disease, high blood pressure, type 2 diabetes, gallstones, breathing problems, and certain cancers".
Based on this information, we can hypothesize that individuals with a higher BMI are likely to have more medical costs. Let's use our data and see if this holds true. Create a scatter plot of charges
(y-axis) versus bmi
(x-axis).
In the scaffolding we provide below, we suggest that you set alpha
to a value between 0.2 and 0.4. alpha
sets the transparency of points on a scatter plot, and increasing transparencing of points is one tool you can use to deal with over plotting issues.
Assign your answer to an object called bmi_plot
. Make sure to label your axes appropriately.
Question 1.6
{points: 3}
Analysis: Comment on the effectiveness of the plot. Take into consideration the rules of thumb discussed in lecture. Also comment on what could be improved for this plot and what is done correctly.
Answer in the cell below.
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.
Question 1.7
{points: 3}
Analysis: What do you observe? Do the data suggest that there might be evidence that BMI may affect the medical costs of individuals?
Answer in the cell below.
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.
Question 1.8
{points: 3}
Again, based on information from the National Heart, Lung and Blood Institute of the US, smoking cigarettes is said to be a risk factor for obesity. Create the same plot as you did in Question 1.5 but this time add the colour
aesthetic to observe if smoking might affect the body mass of individuals. Also, use labs
to format your legend title.
Assign your answer to an object called smoke_plot
. Make sure to label your axes appropriately.
Question 1.9.0 (Analyzing the Graph) True or False:
{points: 1}
Smokers generally have a lower BMI than non-smokers.
Assign your answer to an object called answer1.9.0
.
Question 1.9.1 (Analyzing the Graph) True or False:
{points: 1}
Smokers generally have higher medical charges than non-smokers.
Assign your answer to an object called answer1.9.1
.
Question 1.10
{points: 1}
Finally, create a bar graph that displays the proportion of smokers for both females and males in the data set. Use sex as the horizontal axis, and colour the bars to differentiate between smokers / nonsmokers. This could, for example, be used help us determine whether we should consider smoking behaviour when exploring whether there is a relationship between sex and medical costs.
Assign your answer to an object called bar_plot
. Make sure to label your axes appropriately.
Question 1.11
{points: 1}
Based on the graph, is the proportion of smokers higher amongst men or women?
Assign your answer to an object called answer1.11
.
2. Color Palettes (beyond the defaults)
{points: 3}
In the worksheet and this tutorial, you have seen the same colours again and again. These are from the default ggplot2
color palette. What if you want different colors? We can do this! In R, one of the libraries that provides altenative color palettes is the RColorBrewer
library.
For this question:
Load the
RColorBrewer
libraryPrint the list of palettes available for you with the
display.brewer.all()
functionYou can also print out a list of color blind friendly palettes with
display.brewer.all(colorblindFriendly = T)
Choose one of the palettes and apply it to the plot whose code is given in the cell below.
For the fill aesthetic with categorical variable the function is:
scale_fill_brewer(palette = '...')
For the fill aesthetic with numeric variable the function is:
scale_fill_distiller(palette = '...')
Finally, you can also use this color blindness simulator to check if your visualizations are color blind friendly
You can look more in depth into the documentation of the scale_fill_*
functions here: https://ggplot2.tidyverse.org/reference/scale_brewer.html
Assign your answer to an object called diamonds_plot
.
3. Fast-Food Chains in the United States (Continued)
{points: 6}
In Worksheet 04, we explored this data set through some visualizations. Now, it is is all up to you. The goal of this assignment is to create one plot that can help you figure out which restaurant to open and where! After that you need to write a paragraph explaining your visualization and why you chose it. Also, explain your conclusion from the visualization and reasoning as to how you came to that conclusion. If you need to bring in outside information to help you answer your question, please feel free to do so. Finally, if there is some way that you could improve your visualization, but don't yet know how to do it, please explain what you would do if you knew how.
In answering this question, there is no need to restrict yourself to the west coast of the USA. Consider all states that you have data for. You have a variety of graphs to choose from, but before starting the assignment, discuss with a partner which plot would be the most optimal to answer this question.
Hint: The function pull
from the dplyr
package selects a column in a data frame and transforms it into a vector. Note: There are different ways you can complete this question so you don't necessarily need to use pull
(you may find a solution without using it) but it may be helpful.
Write a paragraph explaining your visualization and why you chose it. Also explain your conclusion from the visualization and reasoning as to how you came to that conclusion. If you need to bring in outside information to help you answer your question, please feel free to do so. Finally, if there is some way that you could improve your visualization, but don't yet know how to do it, please explain what you would do if you knew how.
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.