R is the name of the programming language itself and RStudio is a convenient user interface.
The goal of this lab is to get you started with the computing workflow we’ll use throughout the semester. You’ll get started using writing reproducible reports using RStudio and R Markdown and practicing version control using git and GitHub.
git is a version control system (like “Track Changes” features from Microsoft Word but more powerful) and GitHub is the home for your Git-based projects on the internet (like DropBox but better).
As the labs progress, you are encouraged to explore beyond what the labs dictate; a willingness to experiment will make you a much better programmer and statistician. This lab is a brief review of the fundamental building blocks of R and RStudio: the interface, reading in data, and basic data wrangling and graphing functions. If you’re new to R or would like additional practice, there are additional resources listed at the end of the lab instructions.
Today’s lab will be individual to give you a chance to become more familiar with the workflow. In future labs you will work in teams and learn how to collaborate using GitHub to produce a single lab report as a team.
By the end of the lab, you will…
Each of your assignments will begin with steps in this section. You saw your TA demonstrate these steps, and they’re outlined in detail below. Going forward, each lab will begin with a “Getting started” section but the details will become more sparse as the semester progresses. You can always refer back to this lab for a detailed list of the steps involved for getting started with an assignment.
Click on the green Code button, select Use HTTPS (this is the default). Click on the clipboard icon to copy the repo URL.
Next, open the RStudio Docker container. To do so,
In RStudio, go to File ➡️ New Project ➡️ Version Control ➡️ Git.
Copy and paste the URL of your assignment repo into the dialog box Repository URL. You can leave Project Directory Name empty. It will default to the name of the GitHub repo.
Click Create Project, and the files from your GitHub repo will be displayed the Files pane in RStudio.
There is one more thing we need to do before getting started on the assignment. Specifically, we need to configure your git so that RStudio can communicate with GitHub. This requires two pieces of information: your name and email address.
To do so, you will use the use_git_config()
function from the usethis
package.
Type the following lines of code in the console in RStudio filling in your name and the email address associated with your GitHub account.
For example, mine would be
Now you’re ready to start! Open the R Markdown file, i.e. the one with the .Rmd extension.
Below are the general components of an RStudio project.
Below are the general components of an RMarkdown file.
To get more details about the RStudio project, RMarkdown file, and just R in general, read Getting Started in Data Visualization by Kieran Healy.
The top portion of your R Markdown file (between the three dashed lines) is called YAML. It stands for “YAML Ain’t Markup Language”. It is a human friendly data serialization standard for all programming languages. All you need to know is that this area is called the YAML (we will refer to it as such) and that it contains meta information about your document.
Before we introduce the data, let’s update the name and date in the YAML.
Open the R Markdown (Rmd) file in your project, input your name for the author name and today’s date for the date, and knit the document.
Next, go to the Git pane in RStudio.
If you have made changes to your Rmd file, you should see it listed here. Click on it to select it in this list and then click on Diff. This shows you the difference between the last committed state of the document and its current state with your changes. If you’re happy with these changes, write “Update author name” in the Commit message box and click Commit.
You don’t have to commit after every change, this would get quite cumbersome. You should consider committing states that are meaningful to you for inspection, comparison, or restoration. In the first few assignments, we will provide some guidance on when to commit and an example commit message.
Now that you have made an update and committed this change, it’s time to push these changes to the web! Or more specifically, to your repo on GitHub. Why? So that others can see your changes. And by others, we mean the course teaching team. (Your repos in this course are private and can only be seen by you and the teaching team.)
In order to push your changes to GitHub, click Push. This will prompt a dialogue box where you first need to enter your user name, and then your password. This might feel cumbersome. Bear with me… We will teach you how to save your password so you don’t have to enter it every time. But for this one assignment you’ll have to manually enter each time you push in order to gain some experience with it.
The Git pane should be empty after you push. If it’s not, click the box next to the remaining files, write an informative commit message, and push.
Now let’s make sure all the changes went to GitHub. Go to your GitHub repo and refresh the page. You should see your commit message next to the updated files. If you see this, all your changes are on GitHub and you’re good to go!
Today’s data is all Ikea furniture. The data was obtained from the TidyTuesday data collection.
Use the code below to read in the data.
The variable definitions are as follows:
variable | class | description |
---|---|---|
item_id | double | item id which can be used later to merge with other IKEA data frames |
name | character | the commercial name of items |
category | character | the furniture category that the item belongs to (Sofas, beds, chairs, Trolleys,…) |
sellable_online | logical | Sellable online TRUE or FALSE |
link | character | the web link of the item |
other_colors | character | if other colors are available for the item, or just one color as displayed in the website (Boolean) |
short_description | character | a brief description of the item |
designer | character | The name of the designer who designed the item. this is extracted from the full_description column. |
depth | double | Depth of the item in Centimeter |
height | double | Height of the item in Centimeter |
width | double | Width of the item in Centimeter |
price_usd | double | the current price in US dollars as it is shown in the website by 4/20/2020 |
Before doing any analysis, you may want to get quick view of the data. This is useful when you’ve imported data to see if your data imported correctly. We can use the View
function to see the entire data set in RStudio. Type the code below in the console to view the entire dataset.
Notice that the View(ikea)
command was run in the console and not in a code chunk in the RMarkdown file. By running the View(ikea)
in the console, the dataset displays in your RStudio environment but not in the PDF output from your knitted Rmd file.
Write all code and narrative in your R Markdown file. Write all narrative in complete sentences. Throughout the assignment, you should periodically knit your R Markdown document to produce the updated PDF, commit the changes in the Git pane, and push the updated files to GitHub.
Tip: Make sure we can read all or your code in your PDF document. This means you will need to break up long lines of code. One way to help avoid long lines of code is is start a new line after every pipe (%>%
) and plus sign (+
).
View
function helped us get a quick view of the dataset, but let’s get more detail about its structure. Viewing a summary of the data is a useful starting point for data analysis, especially if the dataset has a large number of observations (rows) or variables (columns). Run the code below to use the glimpse
function to see a summary of the ikea
dataset.How many observations are in the ikea
dataset? How many variables?
Let’s begin by looking at the price of Ikea furniture. Use the code below to visualize the distribution of price_usd
, the price in US dollars.
Use the visualization to describe the distribution of price. In your narrative, include description of the shape, approximate center, approximate spread, and any presence of outliers.
ggplot(data = ikea, aes(x = price_usd)) +
geom_histogram() +
labs(x = "_____",
y = "_____",
title = "_____")
See the ggplot2 reference on density plots for code examples.
price_usd
. Be sure to include an informative title and informative axis labels.Check your lab-01 repo on the GitHub website (you may need to refresh the page in your browser) to ensure that all of the files are up-to-date.
This is a good place to knit, commit, and push changes to your remote lab-01 repo on GitHub. Write an informative commit message (e.g. “Completed exercises 1 - 4”), and push every file to GitHub by clicking the checkbox next to each file in the Git pane. After you push the changes, the Git pane in RStudio should be empty."
We are most interested in the relationship between two or more variables, so let’s begin by looking at the distribution of price by category. We’ll focus on the five categories in the code below, since these include commonly purchased types of furniture.
Use the code below to name the new data frame that only includes the furniture categories of interest. We’re giving this subset a new name, so we don’t overwrite the original data.
You will use this new data frame for the remainder of the lab.
ikea_sub <- ikea %>%
filter(category %in% c("Tables & desks", "Bed",
"Bookcases & shelving units",
"Sofas & armchairs", "Children's furniture"))
The overlapping colors make it difficult to tell what’s happening with the distributions for the categories plotted first and hence covered by categories plotted over them. We can change the transparency level of the fill color to help with this. The alpha argument takes values between 0 and 1: 0 is completely transparent and 1 is completely opaque. There is no way to tell what value will work best, so it’s best to try a few.
Recreate the density plot using a more suitable alpha level, so we can more easily see the distribution of all the categories. Include an informative title and informative axis labels.
Briefly describe why we defined the fill of the curves by mapping aesthetics of the plot (inside the aes
function) but we defined the alpha level as a characteristic of the plotting geom.
Overlapping density plots are not the only way to visualize the relationship between a quantitative and categorical variable. Use a different type of plot to visualize the relationship between price_usd
and category
. Include an informative title and informative axis labels.
You can use the ggplot2 cheatsheet and from Data to Viz for inspiration.
This is another good place to knit, commit, and push changes to your remote lab-01 repo on GitHub. Write an informative commit message (e.g. “Completed exercises 5 - 8”), and push every file to GitHub by clicking the checkbox next to each file in the Git pane. After you push the changes, the Git pane in RStudio should be empty."
Use your visualization to describe the relationship between the width and price of Ikea furniture.
ggplot(data = _____, aes(x = width, y = _____)) +
geom_point() +
labs(x = "_____",
y = "_____",
title = "_____")
You’re done and ready to submit your work! Knit, commit, and push all remaining changes. You can use the commit message “Done with Lab 1!”, and make sure you have pushed all the files to GitHub (your Git pane in RStudio should be empty) and thatall documents are updated in your repo on GitHub. The PDF document you submit to Gradescope should be identical to the one in your GitHub repo.
See the instructions below to submit your work on Gradescope.
In this class, we’ll be submitting PDF documents to Gradescope.
Before you wrap up the assignment, make sure all documents are updated on your GitHub repo. We will be checking these to make sure you have been practicing how to commit and push changes.
Remember – you must turn in a PDF file to the Gradescope page before the submission deadline for full credit.
To submit your assignment:
Go to http://www.gradescope.com and click Log in in the top right corner.
Click School Credentials ➡️ Duke NetID and log in using your NetID credentials.
Click on your STA 210 course.
Click on the assignment, and you’ll be prompted to submit it.
Mark the pages associated with each exercise. All of the pages of your lab should be associated with at least one question (i.e., should be “checked”).
Select the first page of your .PDF submission to be associated with the “Overall” section.
Chapter 2: Get Started Data Visualization by Kieran Healy
Chapter 3: Data visualization in R for Data Science by Hadley Wickham
RStudio Cloud Primers