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

HW02_Bei Wang resubmission #44

Open
wants to merge 1 commit 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
13 changes: 13 additions & 0 deletions HW02_4.Rproj
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
66 changes: 53 additions & 13 deletions HW02_A_Graph-Fails.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "What went wrong?"
author: "Robert Gruener"
title: "Corrected graphs, answers, and comments"
author: Bei Wang
date due: "7/13/2020"
output: github_document
---
Expand Down Expand Up @@ -29,20 +29,32 @@ library("magrittr") #so I can do some piping

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

**My answer/comment:**

Choose a reason for hiding this comment

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

Nice way of highlighting your answers and comments! I will try using it in my code.


*the following 2 errors have been corrected.*

*1.error 1: use "+"" instead of "%>%"*

*2.error 2: "city" was misspelled; there is a variable called "cty" that means city miles per gallow.*
```{r}
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 :`(

**My answer/comment:**

*It seems the color command should be wrapped in geom_point(), and my guess is this is because color belongs to this layer instead of ggplot(). I'm not so sure about this.*

```{r}
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(color = "blue")

```

Expand All @@ -51,36 +63,64 @@ ggplot(data = mpg) +
### 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?


**My answer/comment:**

*alpha is for degree of transparency of the points, which ranges from 0(transparent) to 1(opaque). Using "size=2" can make the points bolder.*

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?

**My answer/comment**

*The position of the legend can be changed by using theme(legend.position = c()), and I found (0.7.0.9) is a good position for this graph. *

*The legend title can be removed by using theme(legend.title = element_blank())*

*Using them(legend.position= "none") could remove the legend*


```{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(0.7, 0.9))+
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?

**My answer/comment**

*Wrapping color=drv in ggplot() will apply the color to every layerl, which is why there were three seperate lines. To have one line, I moved the color=drv to the geom_point. This way it's only applied to the points.*

*I also noticed an interesting fact that the block of codes below would fail with an error message saying "%>% cannot be found" and wont if I didnt run the library("magrittr") in the very first r code block in this file.*

```{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
mpg %>%
ggplot(mapping = aes(x = displ, y=hwy)) +
geom_point(mapping = aes(color = drv)) +
geom_smooth(se = F)
```

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

**My answer/comment**

*Using fill instead of color will change the box colors.*

*Using hjust = 1 will adjust the x-axis labels to a proper position without overlapping with the graph.*

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


Expand Down
130 changes: 130 additions & 0 deletions HW02_A_Graph-Fails.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
Corrected graphs, answers, and comments
================
Bei Wang

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

**My answer/comment:**

*the following 2 errors have been corrected.*

*1.error 1: use "+"" instead of "%>%"*

*2.error 2: "city" was misspelled; there is a variable called "cty" that means city miles per gallow.*

``` 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-markdown_github/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 :\`(

**My answer/comment:**

*It seems the color command should be wrapped in geom\_point(), and my guess is this is because color belongs to this layer instead of ggplot(). I'm not so sure about this.*

``` r
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(color = "blue")
```

![](HW02_A_Graph-Fails_files/figure-markdown_github/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?

**My answer/comment:**

*alpha is for degree of transparency of the points, which ranges from 0(transparent) to 1(opaque). Using "size=2" can make the points bolder.*

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?

**My answer/comment**

*The position of the legend can be changed by using theme(legend.position = c()), and I found (0.7.0.9) is a good position for this graph. *

*The legend title can be removed by using theme(legend.title = element\_blank())*

*Using them(legend.position= "none") could remove the legend*

``` r
mpg %>%
ggplot() +
geom_point(mapping = aes(x = displ, y = hwy, color = class), size = 2) +
theme(legend.direction = "horizontal") +
theme(legend.position = c(0.7, 0.9))+
theme(legend.title = element_blank())
```

![](HW02_A_Graph-Fails_files/figure-markdown_github/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?

**My answer/comment**

*Wrapping color=drv in ggplot() will apply the color to every layerl, which is why there were three seperate lines. To have one line, I moved the color=drv to the geom\_point. This way it's only applied to the points.*

*I also noticed an interesting fact that the block of codes below would fail with an error message saying "%>% cannot be found" and wont if I didnt run the library("magrittr") in the very first r code block in this file.*

``` r
mpg %>%
ggplot(mapping = aes(x = displ, y=hwy)) +
geom_point(mapping = aes(color = drv)) +
geom_smooth(se = F)
```

## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

![](HW02_A_Graph-Fails_files/figure-markdown_github/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?

**My answer/comment**

*Using fill instead of color will change the box colors.*

*Using hjust = 1 will adjust the x-axis labels to a proper position without overlapping with the graph.*

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

![](HW02_A_Graph-Fails_files/figure-markdown_github/unnamed-chunk-5-1.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 49 additions & 4 deletions HW02_B_Mimic_starter.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "HW02_B_Graph-Mimic"
author: "YOUR NAME HERE"
author: "Bei Wang"
output: github_document
---

Expand Down Expand Up @@ -34,6 +34,7 @@ Try for all 4, but if you are struggling don't worry about it. Try your best for

### Graph 1
```{r, echo = T, include = TRUE}

data("diamonds")
#hint think about the *position* the bars are in...
```
Expand All @@ -42,26 +43,47 @@ data("diamonds")
Using the diamonds dataset, make this graph:
```{r graph1 code, echo=FALSE}


ggplot(diamonds, aes(cut, fill = clarity))+
geom_bar(position = "dodge")+
labs(title="My Diamond Collection",
subtitle="Boxplot representing the number of diamonds in my diamon collection by\ntype of cut quality and clarity of diamond")+
theme(plot.title = element_text(hjust=0.5, face="bold"))+
xlab("Dimond Cut")+
ylab("Number of Diamonds")+
annotate("text", x="Premium", y=4500, label="My Best Diamonds\nof course")+
annotate("rect", xmin="Premium", xmax="Ideal", ymin=0, ymax=5000, alpha=0.5)# I don't know how to move the shade and text more to the left/right, since x-aix is discrete.

Choose a reason for hiding this comment

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

I think you can set a value to x/xmin/xmax. I tried it and found that 1 is the center of the first item, 2 is the second, 1.5 is the boundary of the first and the second, etc.

Copy link
Author

Choose a reason for hiding this comment

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

thanks, that's helpful!

```

### Graph 2
```{r, echo = T, include = TRUE}
data("iris")

```

Using the iris dataset, make this graph:
```{r graph 2 code, echo=FALSE}
library(plyr)

```
neworder <- c("versicolor","setosa","virginica")

iris2 <- arrange(transform(iris,
Species=factor(Species,levels=neworder)),Species)#I don't know if there is a simpler way to change order of the panels in facet_wrap

Choose a reason for hiding this comment

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

I used
iris$Species <- factor(iris$Species, levels = c("versicolor","setosa","virginica"))
for changing the order
This might still be complicated if there's lots of species

Copy link
Author

Choose a reason for hiding this comment

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

This seems like a simpler way of changing the order for this specific set!


ggplot(iris2,mapping = aes(x=Sepal.Length, y=Petal.Length, color= Species, shape= Species))+
geom_point()+
geom_smooth(se= F, method="lm", color="black")+
facet_wrap(~Species, scales = "free_y")


```

### Graph 3
You'll need the information in this first box to create the graph
```{r, echo = TRUE}
data("mpg")
corvette <- mpg[mpg$model == "corvette",]
#install
install.packages("ggrepel")
require("ggrepel") #useful for making text annotations better, hint hint
set.seed(42)
```
Expand All @@ -70,6 +92,21 @@ Now using the mpg dataset and the corvette dataset, make this graph:

```{r graoh 3 code}

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

ggplot(mpg, aes(displ,hwy))+
geom_point()+
geom_point(data=corvette, color="blue")+
geom_text_repel(data=corvette,
aes(displ, hwy, label=paste("Corvette", year, sep=",")))+
labs(title="Corvettes are a bit of an outlier")+
scale_x_continuous(limits=c(1,8), breaks= seq(1,8,1))


```

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.
Expand All @@ -91,7 +128,15 @@ 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_fill_brewer(palette = "Set2")+
scale_color_brewer(palette = "Set2")+
geom_boxplot(outlier.alpha=0, alpha=0)+
coord_flip()+
geom_point(aes(color=class), position= position_jitter(height=0))+

Choose a reason for hiding this comment

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

I didn't add height = 0 and I got randomly arranged dots instead of aligned ones. Thanks because now I know how to fix it, but I am still confused about what did setting height to 0 actually change...?

Copy link
Author

Choose a reason for hiding this comment

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

the position_jitter (height=) sets the resolution of the jitters on the y axis; the default is set to 0.4. Here it is actually x-axis because I flipped the coordinate. Since the x axis (Car class) is a categorical variable that composed integers. So in order to have the jitter only be set to integers, height should be set to 0. I didn't find much about this function, so this is just what I think it might be:)

Copy link
Author

Choose a reason for hiding this comment

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

For the dots you got by not setting height=0, I'm guessing its arranged by every 0.4?

labs(title="Horizontal BoxPlot of City MPG and Car Class",
y = "Car Class", x = "City mpg")+
theme_classic()
```


Expand Down
Loading