Data Visualization, part 2. Code for Quiz 8.
Create a plot with the mpg dataset
add points with geom_point
assign the variable displ
to the x-axis
assign the variable hwy
to the y-axis
add facet_wrap
to split the data into panels based on the manufacturer
Create a plot with the mpg
dataset
add bars with geom_bar
-assign the variable manufacturer
to the y-axis
add facet_grid
to split the data into panels based on the class
let scales vary across columns
let space taken up by panels vary by column
To help you complete this question use:
Download the file spend_time.csv
from moodle
spend_time
contains 10 years of data on how many hours Americans spend on each day on 5 activities
read it into spend_time
Start with spend_time
geom_col
activity
to the x-axisavg_hours
to the y-axisactivity
to fillscale_y_continuous
with breaks every hour from 0 to 6 hourslabs
to
subtitle
to Avg hours per day: 2013x
and y
to NULL so they won’t be labeledp1
p1
Start with spend_time
geom_col
year
to the x-axisavg_hours
to the y-axisactivity
to filllabs
to
p2
p2
Use patchwork to display p1
on top of p2
assign the output to p_all
display p_all
Start with p_all
legend.position
to ‘none’ to get rid of the legendp_all_no_legend
p_all_no_legend
Start with p_all_no_legend
see how to annotate the composition here: https://patchwork.data-imaginist.com/reference/plot_annotation.html
ADD plot_annotation
set
title
to “How much time Americans spent on selected activities”caption
to “Source: American Time of Use Survey, https://data.bls.gov/cgi-bin/surverymost?tu”use spend_time
from last quaestion patchwork slides
Start with spend_time
geom_point
year
to the x-axisavg_hours
to the y-axisgeom_smooth
year
to the x-axisavg_hours
to the y-axisscale_x_continuous
labs
to
subtitle
to Avg hours per day: food prepx
and y
to NULL so x and y axes won’t be labeledp4
p4
Start with p4
coord_cartesian
to change range on y axis to 0 and 6p5
p5
Start with spend_time
geom_point
year
to the x-axisavg_hours
to the y-axisactivity
to coloractivity
to groupgeom_smooth
avg_hours
to the y-axisactivity
to coloractivity
to groupscale_x_continuous
coord_cartesian
to change range on y axis to 0 to 6labs
to
x
and y
to NULL so they won’t be labeledp6
p6
Use patchwork to display p4
and p5
on top of p6