-
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
Lucas Dyer HW02 #24
base: master
Are you sure you want to change the base?
Lucas Dyer HW02 #24
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,6 +1,6 @@ | ||
--- | ||
title: "What went wrong?" | ||
author: "Robert Gruener" | ||
author: "Lucas Dyer" | ||
date due: "7/13/2020" | ||
output: github_document | ||
--- | ||
|
@@ -33,16 +33,17 @@ What error is being thrown? How do you correct it? (hint, the error message tell | |
data(mpg) #this is a dataset from the ggplot2 package | ||
|
||
mpg %>% | ||
ggplot(mapping = aes(x = city, y = hwy, color = "blue")) %>% | ||
ggplot(mapping = 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} | ||
ggplot(data = mpg) + | ||
geom_point(mapping = aes(x = displ, y = hwy, color = "blue")) | ||
|
||
ggplot(data = mpg, aes(displ, hwy)) + | ||
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. For me, I left the aes() function inside the geometries layer, but yeah, putting them inside ggplot() could be tidier. |
||
geom_point(colour = "blue") | ||
|
||
``` | ||
|
||
|
@@ -52,22 +53,28 @@ ggplot(data = mpg) + | |
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? | ||
|
||
The alpha refers to something's transparency. It is a scale from 0-1, so setting it to 2 should render it fully opaque | ||
To remove the plot legend entirely, you would use theme(legend.position = "none") | ||
```{r} | ||
mpg %>% | ||
ggplot() + | ||
geom_point(mapping = aes(x = displ, y = hwy, color = class), alpha = 2) + | ||
geom_point(mapping = aes(x = displ, y = hwy, color = class), size = 2) + | ||
theme(legend.direction = "horizontal") + | ||
theme(legend.position = c(5, 40)) | ||
theme(legend.position = c(.6, .80))+ | ||
theme(legend.title = element_blank()) | ||
|
||
``` | ||
|
||
### 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? | ||
|
||
This is happening because the color aesthetic was too far up the hierarchy. Applying it to only the point layer fixes this | ||
|
||
```{r} | ||
mpg %>% | ||
ggplot(mapping = aes(x = displ, y = hwy, color = drv)) + | ||
geom_point() + | ||
ggplot(mapping = aes(x = displ, y = hwy)) + | ||
geom_point(aes(color=drv)) + | ||
geom_smooth(se = F) #se = F makes it so it won't show the error in the line of fit | ||
``` | ||
|
||
|
@@ -78,9 +85,10 @@ Also, the x-axis labels were overlaping, so I rotated them. But now they overlap | |
|
||
|
||
```{r} | ||
ggplot(data = mpg, mapping = aes(x = manufacturer, y = cty, color = manufacturer)) + | ||
ggplot(data = mpg, mapping = aes(x = manufacturer, y = cty, color = manufacturer, fill=manufacturer)) + | ||
geom_boxplot() + | ||
theme(axis.text.x = element_text(angle = 45)) | ||
theme(axis.text.x = element_text(angle = 45,))+ | ||
theme(axis.text.x = element_text(vjust=0.6)) | ||
``` | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
What went wrong? | ||
================ | ||
Lucas Dyer | ||
|
||
``` 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 = cty, y = hwy, color = "blue")) + | ||
geom_point() | ||
``` | ||
|
||
![](HW02_A_Graph-Fails_files/figure-gfm/unnamed-chunk-1-1.png)<!-- --> | ||
|
||
### 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, aes(displ, hwy)) + | ||
geom_point(colour = "blue") | ||
``` | ||
|
||
![](HW02_A_Graph-Fails_files/figure-gfm/unnamed-chunk-2-1.png)<!-- --> | ||
|
||
### 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? | ||
|
||
The alpha refers to something’s transparency. It is a scale from 0-1, so | ||
setting it to 2 should render it fully opaque To remove the plot legend | ||
entirely, you would use theme(legend.position = “none”) | ||
|
||
``` r | ||
mpg %>% | ||
ggplot() + | ||
geom_point(mapping = aes(x = displ, y = hwy, color = class), size = 2) + | ||
theme(legend.direction = "horizontal") + | ||
theme(legend.position = c(.6, .80))+ | ||
theme(legend.title = element_blank()) | ||
``` | ||
|
||
![](HW02_A_Graph-Fails_files/figure-gfm/unnamed-chunk-3-1.png)<!-- --> | ||
|
||
### 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? | ||
|
||
This is happening because the color aesthetic was too far up the | ||
hierarchy. Applying it to only the point layer fixes this | ||
|
||
``` r | ||
mpg %>% | ||
ggplot(mapping = aes(x = displ, y = hwy)) + | ||
geom_point(aes(color=drv)) + | ||
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' | ||
|
||
![](HW02_A_Graph-Fails_files/figure-gfm/unnamed-chunk-4-1.png)<!-- --> | ||
|
||
### 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, fill=manufacturer)) + | ||
geom_boxplot() + | ||
theme(axis.text.x = element_text(angle = 45,))+ | ||
theme(axis.text.x = element_text(vjust=0.6)) | ||
``` | ||
|
||
![](HW02_A_Graph-Fails_files/figure-gfm/unnamed-chunk-5-1.png)<!-- --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
title: "HW02_B_Graph-Mimic" | ||
author: "YOUR NAME HERE" | ||
author: "Lucas Dyer" | ||
output: github_document | ||
--- | ||
|
||
|
@@ -27,9 +27,7 @@ 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: | ||
My submission for this part! I welcome any feedback! | ||
|
||
|
||
### Graph 1 | ||
|
@@ -42,6 +40,10 @@ data("diamonds") | |
Using the diamonds dataset, make this graph: | ||
```{r graph1 code, echo=FALSE} | ||
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. I would recommend changing "echo" to TRUE, so that codes will appear right above the graphs in the corresponding .md file, which makes it easier for others to review them! 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. Thank you! I couldn't really remember what that function did, so I just left it as I saw it in anothe example. |
||
|
||
ggplot(diamonds, aes(cut))+ | ||
geom_bar ((aes(color=clarity, fill = clarity)), position = "dodge")+ | ||
labs(y="Number of Diamonds", x="Diamond Cut", title="My Diamond Collection", subtitle="Boxplot representing the number of diamonds in my diamond collection by \ntype of cut and clarity of diamond")+ | ||
theme(plot.title = element_text(hjust=0.5)) | ||
|
||
``` | ||
|
||
|
@@ -52,7 +54,12 @@ data("iris") | |
|
||
Using the iris dataset, make this graph: | ||
```{r graph 2 code, echo=FALSE} | ||
|
||
iris$Species2 <-factor(iris$Species, levels=c("versicolor", "setosa", "virginica")) | ||
ggplot(iris, aes(Sepal.Length, Petal.Length))+ | ||
geom_point(aes(color=Species, shape=Species))+ | ||
facet_wrap(~Species2, scales = "free_y")+ | ||
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. Thanks, this line has been really helpful, since I did not know that the axes of faceted figures can be synced in only one dimension. 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. By the way, it might be a good idea to keep it coherent in how spaces are added, e.g., comparing to line 60, I would recommend "=" to be wrapped with spaces also in line 59. |
||
geom_smooth(method=lm,se=FALSE, color="black") | ||
|
||
``` | ||
|
||
|
||
|
@@ -69,6 +76,14 @@ set.seed(42) | |
Now using the mpg dataset and the corvette dataset, make this graph: | ||
|
||
```{r graoh 3 code} | ||
require("ggrepel") | ||
data("mpg") | ||
corvette <- mpg[mpg$model == "corvette",] | ||
ggplot(mpg, aes(displ,hwy))+ | ||
geom_point()+ | ||
geom_point(data=corvette, color="blue")+ | ||
geom_text_repel(data=corvette, aes(label=paste("Corvette,", year)))+ | ||
labs(title = "Corvettes are a bit of an outlier") | ||
|
||
``` | ||
|
||
|
@@ -91,6 +106,14 @@ The above graph lets you see some colobrlind friendly palettes. For the graph be | |
Now using the above mpg dataset, make this graph | ||
|
||
```{r graph 4 code} | ||
ggplot(mpg, aes(class, cty))+ | ||
|
||
scale_color_brewer(palette="Set2")+ | ||
scale_fill_brewer(palette="Set2")+ | ||
geom_dotplot(binaxis='y', dotsize=0.5, stackdir='center', aes( fill=class, color=class))+ | ||
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. This is an interesting implementation, which creates well-aligned dots in y-direction comparing to the random style using jittering. |
||
coord_flip()+ | ||
geom_boxplot(alpha=0)+ | ||
labs(title="Horizontal BoxPlot of City MPG and Car Class", x="City mpg", y="Car Class") | ||
|
||
``` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
HW02\_B\_Graph-Mimic | ||
================ | ||
Lucas Dyer | ||
|
||
``` 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 | ||
|
||
My submission for this part\! I welcome any feedback\! | ||
|
||
### Graph 1 | ||
|
||
``` r | ||
data("diamonds") | ||
#hint think about the *position* the bars are in... | ||
``` | ||
|
||
Using the diamonds dataset, make this graph: | ||
![](HW02_B_Mimic_starter_files/figure-gfm/graph1%20code-1.png)<!-- --> | ||
|
||
### Graph 2 | ||
|
||
``` r | ||
data("iris") | ||
``` | ||
|
||
Using the iris dataset, make this graph: | ||
|
||
## `geom_smooth()` using formula 'y ~ x' | ||
|
||
![](HW02_B_Mimic_starter_files/figure-gfm/graph%202%20code-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_starter_files/figure-gfm/graoh%203%20code-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_starter_files/figure-gfm/unnamed-chunk-4-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 | ||
|
||
## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`. | ||
|
||
![](HW02_B_Mimic_starter_files/figure-gfm/graph%204%20code-1.png)<!-- --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
# HW02 | ||
Lucas Dyer | ||
Due 7/13/2020 | ||
|
||
There are two parts to the HW this week. | ||
|
||
[The first assignment asks you to fix some common issues with making ggplot plots ](HW02_A_Graph-Fails.Rmd). You can find the code in that link or the file: HW02_A_Graph-Fails.Rmd | ||
|
||
The second part of the HW will likely be more challenging as it asks you to try to recreate the graphs from just the images. [Here is a link to the markdown file](HW02_B_Mimic.md) with the images you will try to recreate or you can just find the file "HW02_B_Mimic.md" or find the html version "HW02_B_Mimic.html" (which would need to be previewed in RStudio or opened up by an internet browser to view properlY) in the repo. To make your lives easier, I also created a "starter" document that has the same code that you can find in the markdown (but obviously the code for the graphs themselves was removed). [Here is a link to the starter document](HW02_B_Mimic_starter.Rmd) or you can look for the file "HW02_B_Mimic_starter.Rmd" | ||
|
||
##HW01 Part 1 is finished and located [Part1](HW02_A_Graph-Fails.md). | ||
\n##HW02 Part 2 is finished and located [Part2](HW02_B_Mimic_starter.md). |
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.
It's a good practice to keep a record of the author, which I did not notice in my HW.