---
title: 'Section 2'
label: 'section'
layout: 'post'
---

# CS100 Section #2

### Plotting in base R

The following are in-class exercises that will help you gain familiarity with plotting in R. Don't worry about having to turn these in: this is all for your own benefit!

Below you'll find commented-out templates for you to fill out. You'll want to uncomment then (remove the #'s) before running them. You can uncomment a highlighted portion of code by pressing Command + Shift + C on Mac or Control + Shift + C on Windows/Linux.

##### Basic Plotting:

Use the ```plot()``` function to plot petal length vs. petal width. Color the data points according to species, and include a legend.
```{r}
# plot(??? ~ ???, col = ???, main = "put a descriptive title here", data = ???)
# legend("topleft", legend = ???, fill = ???)
```

##### Histogram:

Use the ```hist()``` function to plot a histogram of waiting times (```$waiting```) for the ```faithful``` dataset.
```{r}
# hist(???,
# main = "put a descriptive title here",
# cex.main = 1.75,
# xlab = "put a descriptive label here",
# ylab = "put a descriptive label here",
# cex.lab = 1.25,
# col = "blue",
# border = "orange")
```

##### Stem and Leaf plots:

Use the ```stem()``` function to generate stem and leaf plots for the 'waiting' and 'eruptions' variables.
```{r}
# stem(???)
# stem(???)
```

##### Bar Charts:

Plot the population of younger people of each race using the ```barplot()``` function. Then, do the same for the population of all people.
```{r}

races_younger<-c("Black", "Hispanic", "Other", "White")
races_all<-c("Native","Asian","Black","Hispanic","Two","White")

population_in_millions_younger<-c(10.76, 19.03, 7.76, 40.50)
population_in_millions_all<-c(1.39, 18.12, 38.60, 55.61, 5.67, 195.35)

# barplot(???,
# names.arg = ???,
# main = "put a descriptive title here",
# ylab = "put a descriptive label here")
```

##### Pie Charts:

Plot populations as a proportion with the ```pie()``` function.
```{r}
races_younger<-c("Black", "Hispanic", "Other", "White")
races_all<-c("Native","Asian","Black","Hispanic","Two","White")

population_in_millions_younger<-c(10.76, 19.03, 7.76, 40.50)
population_in_millions_all<-c(1.39, 18.12, 38.60, 55.61, 5.67, 195.35)

pie_labels_younger <- 
  paste(races_younger, 
  population_in_millions_younger, 
  sep = "\n")

# pie(???,
# main = "put a descriptive title here",
# labels = ???)

```

##### Dot Plots:

Use ```dotchart()``` to create a dot plot for the income per captia (```$Income```) of each state
```{r}
states <- data.frame(state.x77)
sorted_states <- 
  states[order(states$Income), ]

# dotchart(???,
# rownames(???),
# cex.lab = .25,
# main = "put a descriptive title here")

```

##### Box Plots:

Use ```boxplot()``` to plot petal lengths of irises by species. Then, with the  ```ToothGrowth``` dataset, plot a notched boxplot with the tooth length (```$len```) on the y-axis against combinations of supplement (```$supp```) and dose (```$dose```) on the x-axis. Hint: you can use * between two features to get a list of combinations between them. This is called a cross product!
```{r}
# boxplot(???,
# main = "put a descriptive title here",
# data = ???,
# xlab = "put a descriptive label here",
# ylab = "put a descriptive label here")

# boxplot(??? ~ ??? * ???,
# data = ???,
# notch = ???,
# col = (c("gold", "darkgreen")),
# main = "put a descriptive title here",
# xlab = "put a descriptive label here")
```

##### Scatterplots:

Plot data points of the ```USJudgeRatings``` dataset on a coordinate grid with ```plot()```. Use x-axis ```FAMI``` (familiarity with law) and y-axis ```RTEN``` (Worthy of Retention).
```{r}
# plot(???,
# xlab = "put a descriptive label here",
# ylab = "put a descriptive label here",
# main = "put a descriptive titile here",
# pch = "+")
```

##### Scatterplot Matrices:

Create multiple scatterplots for the ```USJudgeRatings``` dataset between features ```CONT``` (Contacts), INTG (Integrity), ```DMNR``` (Demeanor) and ```DILG``` (Diligence) with pairs().
```{r}
features <- c("Contacts", "Integrity", "Demeanor", "Diligence")

# pairs(~ ??? + ??? + ??? + ??? ,
# labels = ???,
# main = "put a descriptive title here")
```

##### 3D Scatterplots:

Use ```scatterplot3d()``` to create a 3-dimensional scatterplot on the ```mtcars``` dataset for the features ```wt```, ```disp```, and ```mpg```.
```{r}
library(scatterplot3d)

# scatterplot3d(???, ???, ???,
# main = "put a descriptive title here")
```


### ggplot2

Use ggplot's ```geom_point()``` to plot the ```txhousing``` dataset, with ```listings``` (active house listings) on the x-axis and ```sales``` (number of sales) on the y-axis. Then, use ```geom_bar``` on the ```midwest``` dataset, which contains counties in the midwest, to plot the number of counties in each state.
```{r}
library(ggplot2)

# ggplot(data = ???, aes(x= ???, y = ???)) + ??? + labs(
#   title="put a descriptive title here", 
#   x="put a descriptive label here",
#   y="put a descriptive label here")

# ggplot(data = ???, aes(x = ???)) + ???
```
