Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monty Kwan HW02 #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
233 changes: 146 additions & 87 deletions HW02_A_Graph-Fails.Rmd
Original file line number Diff line number Diff line change
@@ -1,87 +1,146 @@
---
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))
```



---
title: "What went wrong?"
author: "Monty Kwan edits"
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()

```
**My answer: This code _pipes_ the first layer of the graph into the 'geom_point' layer, which is wrong. Instead, we need to use '+' to _add_ the 'geom_point' layer to the graph. Also, look at the data columns in data(mpg), and 'city' is spelled 'cty' so this needs to be adjusted in the aes(x=cty). Are _data_ = mpg and _mapping_ = aes necessary in the code??**

#### My corrected graph 1:

```{r}
data(mpg) #this is a dataset from the ggplot2 package

ggplot(mpg, aes(x = cty, y = hwy, color = "blue")) +
Copy link

@caichufan caichufan Jul 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is correct!!

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"))

```
<br>**My answer: To change the color of the 'geom_point' layer, the 'aes' can't include the 'color' attribute _inside_ the ( ). If the 'color' attribute is defined within the aesthetic, ggplot will think 'color' is a feature of a variable. The 'color' needs to be defined in 'geom_point'.**

#### My corrected graph 2:
```{r}
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
```
<br> Either code works
```{r}
ggplot(mpg, aes(x=displ, y=hwy)) +
geom_point(color="blue")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!!

```

### 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?

<br>**My answer: Alpha (has values between 0-1, where 0=invisible and 1=darkest) that controls transparency of points in 'geom_point', for example. Setting alpha to 2 would not do anything more than setting it to 1. To make the points slightly bigger, the 'size' aesthetic can bet set as an argument within 'geom_point'.**

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?

<br>**My answer: To change stylistic elements of a plot, call 'theme()' and change 'legend.position' using 'c(x, y)', where 'c(0, 0)' means bottom-left of the coordinate plane and 'c(0,1)' means top-right. To remove the legend title ("class"), use the 'element_blank()' function in a theme layer: theme(legend.title=element_blank()). To remove the plot legend completely: theme(legend.position="none"). Alternatively, would theme(legend.position=element_blank()) also work??**
```{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))

```

#### My corrected graph 3:
```{r}
mpg %>%
ggplot() +
geom_point(mapping = aes(x = displ, y = hwy, color = class), size = 3) +
theme(legend.direction = "horizontal") +
theme(legend.position = c(.6, .8)) +
theme(legend.title = element_blank())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!!


```

### 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?

<br>**My answer: Since 'color=drv' is defined within the ggplot function, it is applied to all layers of the graph, including the smoothing lines. To tell ggplot you don't want the lines to be affected by the different categories of 'drv', it should only be defined within 'geom_point'**

```{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
```

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Monty,

I ran your code, and it is giving the same result as mine! what I have for the last line: geom_smooth(se = F, aes(color = NULL))
so it just commended to ignore the color differences from groups, and draw the line.

Your way def works well too!

```

### 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?
<br>**My answer: 'color' only gives instructions for the outline, but 'fill' defines the fill color of the box. Not sure how to make all the boxes the same color??**

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?
<br> **My answer: To adjust the positioning of the x-axis title, add vjust (for vertical justification from 0-1).**


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

#### My corrected graph 5 with all boxes one color:
```{r}
ggplot(data = mpg, mapping = aes(x = manufacturer, y = cty)) +
geom_boxplot(fill="#7dd1e8") +
theme(axis.text.x = element_text(angle = 45, vjust=0.5))
```

#### My corrected graph 5 with the box one color with each manufacturer having diff color:
```{r}
ggplot(data = mpg, mapping = aes(x = manufacturer, y = cty, fill=manufacturer)) +
geom_boxplot() +
theme(axis.text.x = element_text(angle = 45, vjust=0.5))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!!

```
Loading