-
Notifications
You must be signed in to change notification settings - Fork 46
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
Ariana Tortolani HW02 #13
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
.Ruserdata |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Version: 1.0 | ||
|
||
RestoreWorkspace: Default | ||
SaveWorkspace: Default | ||
AlwaysSaveHistory: Default | ||
|
||
EnableCodeIndexing: Yes | ||
UseSpacesForTab: Yes | ||
NumSpacesForTab: 2 | ||
Encoding: UTF-8 | ||
|
||
RnwWeave: Sweave | ||
LaTeX: pdfLaTeX |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,153 @@ | ||
--- | ||
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: "Ariana Tortolani" | ||
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} | ||
|
||
#ORIGINAL CODE WITH ERROR: | ||
data(mpg) #this is a dataset from the ggplot2 package | ||
|
||
mpg %>% | ||
ggplot(mapping = aes(x = city, y = hwy, color = "blue")) %>% | ||
geom_point() | ||
|
||
#EDITED CODE: | ||
data(mpg) #this is a dataset from the ggplot2 package | ||
|
||
ggplot(mpg, aes(x = cty, 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} | ||
|
||
#ORIGINAL CODE WITH ERROR: | ||
ggplot(data = mpg) + | ||
geom_point(mapping = aes(x = displ, y = hwy, color = "blue")) | ||
|
||
#EDITED CODE: | ||
ggplot(data = mpg, aes(x = displ, y = hwy)) + | ||
geom_point(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? | ||
|
||
ANSWER: | ||
Changing the alpha value changes the transparency of the points: | ||
alpha = 1 : opaque | ||
alpha = 0 : transparent | ||
|
||
i am not sure what setting alpha = 2 does. i can not see any changes when i generate the plot | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on the tutorial on DataCamp, setting the alpha = 2 should make the points appear at 20% transparency. |
||
|
||
you could use size to make the points larger | ||
|
||
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? | ||
|
||
ANSWER: | ||
c(1,1) - places the legend in the upper right hand corner, start there and then adjust the numbers untl in the right location | ||
add legend.title = element_blank() to remove the title | ||
change legend.position = "none" to remove the plot legend entirely | ||
```{r} | ||
#ORIGINAL CODE WITH ERROR | ||
mpg %>% | ||
ggplot() + | ||
geom_point(mapping = aes(x = displ, y = hwy, color = class), alpha = 2) + | ||
theme(legend.direction = "horizontal") + | ||
theme(legend.position = c(5, 40)) | ||
|
||
#EDITED CODE | ||
mpg %>% | ||
ggplot() + | ||
geom_point(mapping = aes(x = displ, y = hwy, color = class), size = 3) + | ||
theme(legend.direction = "horizontal") + | ||
theme(legend.position = c(0.6,0.85)) + | ||
theme(legend.title = element_blank()) | ||
|
||
#to remove the legend completely | ||
mpg %>% | ||
ggplot() + | ||
geom_point(mapping = aes(x = displ, y = hwy, color = class), size = 3) + | ||
theme(legend.direction = "horizontal") + | ||
theme(legend.position = "none") | ||
|
||
``` | ||
|
||
|
||
### 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? | ||
|
||
ANSWER: | ||
so when i removed color = dry the three different color distincts disappear and there is one smooth line going across the entire dataset. i am not sure if there is a way to have one smooth line going across the data with it being grouped by the three different categories? | ||
|
||
```{r} | ||
#ORIGINAL CODE | ||
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 | ||
|
||
#EDITED CODE | ||
mpg %>% | ||
ggplot(mapping = aes(x = displ, y = hwy)) + | ||
geom_point() + | ||
geom_smooth(se = F) #se = F makes it so it won't show the error in the line of fit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Ariana, I was facing the same problem you had and I found this helpful post on stack overflow. You can refer to this link https://stackoverflow.com/questions/47883446/plot-a-single-geom-smooth-curve-instead-of-multiple or my codes for more details but essentially, there are two ways to solve this issue.
|
||
``` | ||
|
||
### 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? | ||
|
||
ANSWER: | ||
Using color will just change the outline, by using fill you can change the entire box 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? | ||
|
||
ANSWER: | ||
Use hjust to change the position of the text on the x axis | ||
|
||
|
||
```{r} | ||
#ORIGINAL CODE | ||
ggplot(data = mpg, mapping = aes(x = manufacturer, y = cty, color = manufacturer)) + | ||
geom_boxplot() + | ||
theme(axis.text.x = element_text(angle = 45)) | ||
|
||
#EDITED CODE | ||
ggplot(data = mpg, mapping = aes(x = manufacturer, y = cty, fill = manufacturer)) + | ||
geom_boxplot() + | ||
theme(axis.text.x = element_text(angle = 45, hjust = 1)) | ||
``` | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
What went wrong? | ||
================ | ||
Robert Gruener | ||
|
||
``` r | ||
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 | ||
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() | ||
``` | ||
|
||
## Error: `mapping` must be created by `aes()` | ||
## Did you use %>% instead of +? | ||
|
||
### 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 | ||
``` | ||
|
||
## `geom_smooth()` using method = 'loess' and formula 'y ~ x' | ||
|
||
<!-- --> | ||
|
||
### 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)) | ||
``` | ||
|
||
<!-- --> |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was what I put down for mine as well! :)