Welcome to Data Visualization in R!

Graphics Assignment 1: GGplot Basics

Welcome to your first graphics challenge! Follow these instructions, step by step, to produce the intended graphic. We will continue to build on this, getting more and more advanced as the semester goes on.

We are going to start by getting your system set up to create your code for the semester. You will be working from this single file, all semester long! This will allow you to keep it for use in the future.

Setting up R and RStudio
  1. Let’s make sure you have R set up and ready to go! Use the directions in Chapter 1 of the text to do so. Here is some code to help you through it. You must have R and Rstudio set up before you can move on.

If you have not previously done so, go to this webpage and download R: http://cran.r-project.org/

Then go to this webpage and download Rstudio: http://www.rstudio.com/products/RStuido

Once you have done this, open RStudio.

Setting up R Markdown
  1. Now we are going to start by getting R markdown all set up. R markdown is a cool and useful tool that allows you to write code, submit your code, and also upload your results all at one time, as a web browser document! It makes it easier to grade, spot mistakes, and allows you to save your submitted assignments. It can take a bit to learn, but I promise it is worth it!

Here are some helpful resources in aiding you to understand R Markdown.

Video Tutorial: https://rmarkdown.rstudio.com/authoring_quick_tour.html

Help Page: https://rmarkdown.rstudio.com/lesson-15.HTML

Here is a PDF version that I find really helpful! https://posit.co/wp-content/uploads/2022/10/rmarkdown-1.pdf

#Run the following code in your console (directly in the code running area) to download the package that allows you to create R markdown files. This code will download the package if you do not have it, and skip the download if you have already done so previously.

if (!require("rmarkdown")) {
  install.packages("rmarkdown", dependencies = TRUE)
  library(rmarkdown)
}

if (!require("knitr")) {
  install.packages("knitr")
  library(knitr)
}

if (!require("tinytex")) {
  install.packages("tinytex")
  library(tinytex)
  tinytex::install_tinytex()  # Install TinyTeX distribution
}
  1. Click on the icon that looks like a white page with a little “+” on it on the top left hand corner of the program, and select “R Markdown”. This choice of file type is very important. From this point on we will be working in the “Code Editor”. This is the only way to save your work! Do not write code directly in the console.
  • You can erase everything after line 6. But make sure you leave the header on there!
  1. Replace the “Untitled1” title with “YOURLASTNAME Data Visualization Code”.

  2. Click the little disk to save the file, and name it the same thing. Be very selective of where you save the file, as all of your data will need to be stored in the same location. I recommend creating a folder on your desktop for this course, saving this file there, and exclusively using that folder moving forward. And remember to save your work often.

  3. Place the following code in the file right under the header, but remove the leading hash tags. I have to put those in for the code to display to you. Any code that you write has to go between these two lines, or it will not run or display to me. Anything you write outside of this is interpreted as plain text instead of code. This means that outside of this region, you can write notes, or write me messages just like you would in a word document. You do not need hashtags. It can allow you to keep really neat files (see the help PDF for organization tricks).

  • This is what we do to create a new “chunk” of code.
#```{r,message=F, warning=FALSE}


#```
  1. In your code editing space, which should appear as a grey bar between this new code, read in your necessary packages. If you use this formatting, you can use it each week as we move forward without altering it, although you may need to add new packages to it as we progress. After you paste in the code, press the little green right pointing arrow in the top right hand corner of the green box. This will run all code within the grey chunk.
# List any packages you need to use here
packages <- c("ggplot2", "readr", "tidyverse", "dplyr", "ggpubr")

#Check to see if any of your listed packages need installed
check_install_packages <- function(pkg){
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg, dependencies = TRUE)
    library(pkg, character.only = TRUE)
  }
}

# Download the packages and read in the libraries if necessary
sapply(packages, check_install_packages)
  1. Import your data for today’s activity by using a demo data set called “USArrests”. Use your text to figure this out. Remember, you can start with data("").

  2. Look at the data, and in the “open text” space of this R Markdown document, write a description of the variables you see. You can type this out just as you would in a word document. Hint: Use the head function to see the data, and the ? function to learn more about it. Be sure to answer:

  • What are the variables available
  • How is each variable defined or calculated
  • Is each one numerical or categorical
  1. Now we are going to use this data to make a graph! I am going to give you most of the code to do so, as we learn the basics. Copy this code into a new code chunk. Label that chunk by creating a header in your document. You can do so by putting a “##” followed by one space, and the title of the section. You can make this chunk “GGplot Graphic Code”.
#General format is going to be calling a ggplot, followed by the dataframe name (mtcars), followed by defining the X and Y variables of the graphic.
ggplot(mtcars, aes(x = mpg, y=hp)) +
    #You then indicate the type of graph to make (in this case, a dotplot using points).
    geom_point()

- These are the absolute basics of a ggplot. You have to tell it what data to use, what variables to choose, and what type of graph to make.

  1. Using your text and any other resources you need, I want you to take this graph that I have given you and do all of the following:
  • Change the dots to a size of 2.4, and star shaped
  • Use the minimal theme to display the graphic
  • Color, or group, the dots by the “cyl” variable. When you do this, keep in mind that you use “color” for continuous data, and “fill” for categorical in the scale_color_manual code.
  • Move the legend to the bottom of the graph.
  • Title your graphic “Effect of Horsepower on Fuel Efficiency”
  • Give a subtitle of “Categorized by Number of Cylinders”
  • Name your X and Y axes “Horsepower” and “Fuel Efficiency (MPG)”

If you have done it correctly, you should get this graphic!

  1. Use any other demo data set to create another graph. You have full freedom to create anything you’d like! Explore other graph types and aesthetic options!

  2. You are now ready to save the assignment to your own webpage! Let me walk you through that. This is how you will be turning in your projects this semester.

  • Go to the top of the window and hit the “Knit” button, making sure it is knitting to HTML.
  • It will show you the product on the right hand side. Make any changes you’d like so that it is neatly formatted and “pretty”. After you make changes, just hit “knit” again to see the adaptations.
  • Hit the “Publish” button above the preview it gives you.
  • Select “Rpubs” and follow the directions for creating your own page. You will have to create an account. Be sure to save your login info.
  • Save the page to your Rpubs by clicking publish. Name it the same thing as this document. Remember, if you make any changes, you will need to republish each time!
  1. Copy and paste the URL of your newly created webpage into the submission box on BB for me to grade it and leave comments.

Graphics Assignment 2: {.tabset .tabset-fade}