R is the name of the programming language itself and RStudio is a convenient interface.
This lab will go through much of the same workflow we demonstrated in class last week. The main goal is to get comfortable in the RStudio environment and using git and GitHub.
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. Before we get to that stage, you need to build some basic fluency in R.
Each of your assignments will begin with similar steps below. You saw these once in class, and they’re outlined in detail here again.
Create your personal private repository corresponding to the assignment by navigating to https://classroom.github.com/a/VJXpY3-l. A repository named lab_01-[github_username]
will be created for you.
Navigate to the repository you just created using the newly provided link.
Click on the green Code button in that repository, and copy the git URL (it should end in .git).
Go to your RStudio Docker container at https://vm-manage.oit.duke.edu/containers/rstudio.
In RStudio, go to File \(\rightarrow\) New Project \(\rightarrow\) Version Control \(\rightarrow\) Git.
Copy and paste the git URL of your assignment repo into the dialog box labeled Repository URL. Adjust the folder to where you want this assignment located.
Click Create Project, and the files from your GitHub repo will be displayed in the Files pane in RStudio.
Click lab_01.Rmd to open the template R Markdown file.
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 much better)
There is one more piece of housekeeping we need to take care of before we get started. Specifically, we need to configure git so that RStudio can communicate with GitHub. This requires two pieces of information: your email address and your name.
To do so, you will use R function use_git_config()
from package usethis
.
Type the following lines of code in the console, but use your name and email address associated with GitHub.
For example, mine would be
In this lab we will work with two packages: datasauRus
which contains the datasets, and tidyverse
which is a collection of packages for doing data analysis in a “tidy” way.
Are both tidyverse
and datasauRus
installed? If not, install them by entering the following code in your console.
Before we introduce the data, we’re going to go through our first stage, commit, and push version control cycle.
The top portion of your R Markdown file (between the three dashed lines) is called the 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.
Open the R Markdown (Rmd) file in your project, change the author name to your name, and knit the document.
Now, go to the Git pane in your RStudio instance. This will be in the top right hand corner in a separate tab.
If you have made and saved changes to your Rmd file, you should see it listed here. Click on Diff. This shows you the difference between the last committed state of the document and its current state that includes your changes. If you’re happy with these changes, stage them, and write “Update author name” in the Commit message box, then click Commit.
Of course, you don’t have to commit after every change, as 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 tell you exactly when to commit and in some cases, what commit message to use. As the semester progresses we will let you make these decisions.
In order to push your changes to GitHub, click on Push. This will prompt a dialog box where you first need to enter your username, and then your password. Later, we’ll configure git so you won’t need to enter your GitHub credentials.
The data frame we will be working with today is called datasaurus_dozen
; it’s in the datasauRus
package. Actually, this single data frame contains 13 datasets, designed to show us why data visualization is important and how summary statistics alone can be misleading. The different datasets are marked by the dataset
variable.
To find out more about the dataset, type the following in your console.
A question mark before the name of an object will bring up its help file.
datasaurus_dozen
have? What are the variables included in the data frame? Add your responses to lab_01.Rmd
. When you’re done, commit your changes with the commit message, “Added answer for Ex 1”, and push to GitHub.Let’s take a look at what these datasets are by making a frequency table of the dataset variable:
## # A tibble: 13 x 2
## dataset n
## <chr> <int>
## 1 away 142
## 2 bullseye 142
## 3 circle 142
## 4 dino 142
## 5 dots 142
## 6 h_lines 142
## 7 high_lines 142
## 8 slant_down 142
## 9 slant_up 142
## 10 star 142
## 11 v_lines 142
## 12 wide_lines 142
## 13 x_shape 142
Matejka, Justin, and George Fitzmaurice. “Same stats, different graphs: Generating datasets with varied appearance and identical statistics through simulated annealing.” Proceedings of the 2017 CHI Conference on Human Factors in Computing Systems. ACM, 2017.
The original Datasaurus (dino
) was created by Alberto Cairo in this blog post. The other Dozen were generated using simulated annealing and the process is described in the paper Same Stats, Different Graphs: Generating Datasets with Varied Appearance and Identical Statistics through Simulated Annealing by Justin Matejka and George Fitzmaurice. In the paper, the authors simulate a variety of datasets that the same summary statistics to the Datasaurus but have very different distributions.
y
vs. x
for the dino
dataset. Also, calculate the correlation coefficient between x
and y
for this dataset.Below is the code you will need to complete this exercise. The answer is already given, but you need to include relevant bits in your Rmd document and successfully knit to view the results.
Start with datasaurus_dozen
and pipe it into the filter()
function to filter for observations where dataset == "dino"
. Store the resulting filtered data frame as a new data frame called dino_data
.
There is a lot going on here, so let’s slow down and unpack it a bit.
First, the pipe operator: %>%
, takes what comes before it and sends it as the first argument to what comes after it. So here, we’re saying filter
the datasaurus_dozen
data frame for observations where dataset == "dino"
.
Second, the assignment operator: <-
, assigns the name dino_data
to the filtered data frame.
Next, we need to visualize these data. We will use the ggplot()
function. Its first argument is the data you’re visualizing. Then we define the aes
thetic mappings. In other words, the columns of the data that get mapped to certain aesthetic features of the plot, e.g. the x
axis will represent the variable called x
and the y
axis will represent the variable called y
. Finally, we add another layer to this plot where we define which geom
etric shapes we want to use to represent each observation in the data. In this case we want these to be points, hence geom_point()
.
For the second part of this exercise, we need to calculate a summary statistic: the correlation coefficient. The correlation coefficient, often referred to as \(r\) in statistics, measures the linear association between two variables. You will see that some of the pairs of variables we plot do not have a linear relationship between them. This is exactly why we want to visualize first: visualize to assess the form of the relationship, and calculate \(r\) only if relevant. In this case, calculating a correlation coefficient really doesn’t make sense since the relationship between x
and y
is definitely not linear.
For illustrative purposes, let’s calculate correlation coefficient between x
and y
.
Start with dino_data
and calculate a summary statistic that we will call r
as the cor
relation between x
and y
.
## # A tibble: 1 x 1
## r
## <dbl>
## 1 -0.0645
This is a good place to pause, commit changes with the commit message, “Added answer for Ex 2”, and push.
y
vs. x
for the star
dataset. You can (and should) reuse code we introduced above, just replace the dataset name with the desired dataset. Then, calculate the correlation coefficient between x
and y
for this dataset. How does this value compare to the r
of dino
?This is another good place to pause, commit changes with the commit message, “Added answer for Ex 3”, and push.
y
vs. x
for the circle
dataset. You can (and should) reuse code we introduced above, just replace the dataset name with the desired dataset. Then, calculate the correlation coefficient between x
and y
for this dataset. How does this value compare to that from dino
?You should pause again, commit changes with the commit message, “Added answer for Ex 4”, and push.
You’re done with the data analysis exercises, but we’d like to do one more things to customize the look of the report. We can customize the output for a particular R chunk by including chunk options.
Use R chunk options to customize the figure output dimensions as specified below.
We can use fig.height
and fig.width
as chunk options to adjust the height and width of figures. Modify the chunks in Exercises 2-4 as follows:
```{r chunk_name, fig.height=5, fig.width=5}
Code that created the figure
```
Once you’ve modified your plots, save and knit. Commit changes with the commit message, “Done with Lab 01”, and push. Verify all your work has been updated on GitHub.
In this class, we’ll be submitting PDF documents to Gradescope. Once you are fully satisfied with your lab, Knit to PDF to create a PDF document. You may notice that the formatting/theme of the report has changed – this is expected.
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 Gradescope before the submission deadline for full credit.
To submit your assignment on Gradescope: