Skip to content

Commit

Permalink
Uploaded the HW documents
Browse files Browse the repository at this point in the history
  • Loading branch information
CFBSD-Summer2020 authored Jul 8, 2020
1 parent 991c68b commit 00fe4a4
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
87 changes: 87 additions & 0 deletions HW02_A_Graph-Fails.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: "What went wrong?"
author: "Robert Gruener"
date due: "7/13/2020"
output: github_document
---

```{r setup, include=TRUE}
knitr::opts_chunk$set(echo = TRUE, error = TRUE)
```

## HW02 Part A

In this document, I will add some examples of some coding mistakes, it is up to you to figure out why the graphs are messing up.

### First load packages

It is always best to load the packages you need at the top of a script. It's another common coding formatting standard (like using the assignment operator instead of the equals sign). In this case, it helps people realize what they need to install for the script and gives an idea of what functions will be called.

It is also best coding practice to only call the packages you use, so if you use a package but end up tossing the code you use for it, then make sure to remove loading it in the first place. For example, I could use `library("tidyverse")` but since this script will only be using ggplot2, I only load ggplot2.

```{r load libraries}
library("ggplot2")
library("magrittr") #so I can do some piping
```


### Graph Fail 1

What error is being thrown? How do you correct it? (hint, the error message tells you)

```{r}
data(mpg) #this is a dataset from the ggplot2 package
mpg %>%
ggplot(mapping = aes(x = city, y = hwy, color = "blue")) %>%
geom_point()
```

### Graph Fail 2
Why aren't the points blue? It is making me blue that the points in the graph aren't blue :`(
```{r}
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))
```



### Graph Fail 3
Two mistakes in this graph. First, I wanted to make the the points slightly bolder, but changing the alpha to 2 does nothing. What does alpha do and what does setting it to 2 do? What could be done instead if I want the points slightly bigger?

Second, I wanted to move the legend on top of the graph since there aren't any points there, putting it at approximately the point/ordered pair (5, 40). How do you actually do this? Also, how do you remove the legend title ("class")? Finally, how would you remove the plot legend completely?
```{r}
mpg %>%
ggplot() +
geom_point(mapping = aes(x = displ, y = hwy, color = class), alpha = 2) +
theme(legend.direction = "horizontal") +
theme(legend.position = c(5, 40))
```

### Graph Fail 4
I wanted just one smoothing line. Just one line, to show the general relationship here. But that's not happening. Instead I'm getting 3 lines, why and fix it please?

```{r}
mpg %>%
ggplot(mapping = aes(x = displ, y = hwy, color = drv)) +
geom_point() +
geom_smooth(se = F) #se = F makes it so it won't show the error in the line of fit
```

### Graph Fail 5
I got tired of the points, so I went to boxplots instead. However, I wanted the boxes to be all one color, but setting the color aesthetic just changed the outline? How can I make the box one color, not just the outline?

Also, the x-axis labels were overlaping, so I rotated them. But now they overlap the bottom of the graph. How can I fix this so axis labels aren't on the graph?


```{r}
ggplot(data = mpg, mapping = aes(x = manufacturer, y = cty, color = manufacturer)) +
geom_boxplot() +
theme(axis.text.x = element_text(angle = 45))
```



89 changes: 89 additions & 0 deletions HW02_B_Mimic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
HW02\_B\_Graph-Mimic
================
Robert Gruener

``` r
library("ggplot2")
library("magrittr") #so I can do some piping
data("diamonds")
data("mpg")
data("iris")
theme_set(theme_bw()) #I'll give you this one, you can set the theme individually for graphs
#or you can set the theme for all the graphs using theme_set()
#theme_bw() is best theme (IMO)

#for graph 3:
library("ggrepel")
```

## HW02 Part B

For this part of the HW, the goal is to try to recreate the graphs I
make from scratch. I will only provide the MD, not the actual code I
used to create it besides which data I use to create it. The rest will
be up to you.

Try for all 4, but if you are struggling don’t worry about it. Try your
best for each, if you don’t get everything that’s what the peer-review
is for. :smile:

### Graph 1

``` r
data("diamonds")
#hint think about the *position* the bars are in...
```

Using the diamonds dataset, make this graph:
![](HW02_B_Mimic_files/figure-gfm/unnamed-chunk-2-1.png)<!-- -->

### Graph 2

``` r
data("iris")
```

Using the iris dataset, make this graph:

## `geom_smooth()` using formula 'y ~ x'

![](HW02_B_Mimic_files/figure-gfm/unnamed-chunk-4-1.png)<!-- -->

### Graph 3

You’ll need the information in this first box to create the graph

``` r
data("mpg")
corvette <- mpg[mpg$model == "corvette",]
#install
require("ggrepel") #useful for making text annotations better, hint hint
set.seed(42)
```

Now using the mpg dataset and the corvette dataset, make this graph:

![](HW02_B_Mimic_files/figure-gfm/unnamed-chunk-6-1.png)<!-- -->

There is a trick to getting the model and year to print off together.
`paste()` is a useful function for this, also pasting together parts of
file names and parts of urls together.

### Graph 4

``` r
data(mpg)

#hint for the coloring, colorbrewer and you can set palette colors and make your graphs colorblind friendly
library(RColorBrewer)
display.brewer.all(colorblindFriendly = T) #take a look at the colorblindfriendly options
```

![](HW02_B_Mimic_files/figure-gfm/unnamed-chunk-7-1.png)<!-- -->

The above graph lets you see some colobrlind friendly palettes. For the
graph below, I used Set2.

Now using the above mpg dataset, make this graph

![](HW02_B_Mimic_files/figure-gfm/unnamed-chunk-8-1.png)<!-- -->

0 comments on commit 00fe4a4

Please sign in to comment.