Day 4 Activities¶
-
Change the
animals
data frame to a tibble calledanimals_tb
. Save the row names to a column calledanimal_names
before turning it into a tibble. -
Use ggplot2 to plot the animal names (x-axis) versus the speed of the animal (y-axis) in
animals_tb
using a scatterplot. Customize the plot to display as shown below. -
We decide that our plot would look better with the animal names ordered from slowest to fastest. Using the
animals_tb
tibble, reorder the animals on the x-axis to start with the slowest animal on the left-hand side of the plot to the fastest animal on the right-hand side of the plot by completing the following steps:a. Use the
arrange()
function to order the rows by speed from slowest to fastest. Then use thepull()
function to extract theanimal_names
column as a vector of character values. Save the new variable asnames_ordered_by_speed
.b. Turn the
animal_names
column ofanimals_tb
into a factor and specify the levels asnames_ordered_by_speed
from slowest to fastest (output in part a). Note: this step is crucial, because ggplot2 usesfactor
as plotting order, instead of the order we observe in data frame.c. Re-plot the scatterplot with the animal names in order from slowest to fastest.
Note
If you are interested in exploring other ways to reorder a variable in ggplot2, refer to this post.
-
Save the plot as a PDF called
animals_by_speed_scatterplot.pdf
to theresults
folder. -
Use the functions from the
dplyr
package to perform the following tasks:a. Extract the rows of
animals_tb
tibble with color of gray or tan, order the rows based from slowest to fastest speed, and save to a variable calledanimals_gray_tan
.animals_gray_tan
as a comma-separated value file calledanimals_tb_ordered.csv
to theresults
folder.
Attribution notice
-
This lesson has been developed by members of the teaching team at the Harvard Chan Bioinformatics Core (HBC). These are open access materials distributed under the terms of the Creative Commons Attribution license (CC BY 4.0), which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited.
-
The materials used in this lesson are adapted from work that is Copyright © Data Carpentry (http://datacarpentry.org/).
-
All Data Carpentry instructional material is made available under the Creative Commons Attribution license (CC BY 4.0).