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

Ben Wang Homework 2 #31

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
13 changes: 13 additions & 0 deletions HW02.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
37 changes: 21 additions & 16 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"
author: "Ben Wang"
date due: "7/13/2020"
output: github_document
---
Expand Down Expand Up @@ -33,19 +33,19 @@ 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()

```

### In the tutorial on DataCamp, we always used '+' for geom_point functions so I tried that here
### I tried running the program but it could not find the object 'city'. The data refers to it as 'cty' instead of 'city', so I changed 'city' to 'cty'
### 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(x = displ, y = hwy)) +
geom_point(color = "blue")

```

### Need to have color separately in 'geom_point()'


### Graph Fail 3
Expand All @@ -55,33 +55,38 @@ Second, I wanted to move the legend on top of the graph since there aren't any p
```{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.65, 0.85)) +
theme(legend.title = element_blank())

```
### Alpha does not change the size, it changes the opacity (0 is transparent, 1 is opaque). We change 'alpha' to 'size' to set size
### Experimented with different coordinates until I got the legend where I wanted it
### Removing legend title wouldn't let us use "none" so "element_blank" worked instead. To completely hide the legend, I could give it a position not seen in the graph (legend.position = c(10000,10000)) or use "none".

Choose a reason for hiding this comment

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

This is interesting, will there be any differences in running time between the two options?

Copy link
Author

Choose a reason for hiding this comment

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

I didn't think of that! Using such large position may cause a longer time running or crash the program. Alternatively, if the program tries to fit everything into one space, perhaps it'll scale the the entire figure really small to accommodate for the huge distance between the figure and legend. Maybe using a different coordinate is not the best option


### 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() +
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
```
### color = drv is responsible for drawing the lines. Since it was inside ggplot(mapping), the lines were drawn indepedently of geom_point and geom_smooth layers. Placing it only in geom_point draws the line according to the point layer only, creating one line for the general relationship.

### 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?
Also, the x-axis labels were overlapping, 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)) +
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))

Choose a reason for hiding this comment

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

Using "hjust" is a good choice! I didn't realize I could use this command to adjust the distance so I just changed the angle to avoid overlapping lol

```


### 'color' changes outline, 'fill' changes actually inside of the box
### use 'hjust' to adjust horizontal axis distance

1 change: 1 addition & 0 deletions HW02_B_Mimic.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ is for. :smile:

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

Expand Down
33 changes: 30 additions & 3 deletions HW02_B_Mimic_starter.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ data("diamonds")

Using the diamonds dataset, make this graph:
```{r graph1 code, echo=FALSE}
library(ggplot2)
### Not sure why, but I only seem to be able to get the code to work after including this library(ggplot2) line here even though it's called earlier in the code?

ggplot(diamonds, aes(x=cut, fill=clarity)) +
geom_bar(position="dodge") +
labs(title = "My Diamond Collection", subtitle = "Boxplot representing the number of diamonds in my diamond collection by type of cut quality and clarity of diamond") +
xlab("Diamond Cut") +
ylab("Number of Diamonds") +
annotate("rect", xmin=4.5, xmax=5.5, ymin=0, ymax=5000, alpha=0.4) +
annotate("text", x=4, y=4500, label = "My Best Diamonds, of course")

```

Expand All @@ -52,7 +61,12 @@ data("iris")

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

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


Expand All @@ -68,8 +82,14 @@ set.seed(42)

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

```{r graoh 3 code}

```{r graph 3 code}
set.seed(42)
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point() +
geom_point(data = corvette, aes(x = displ, y = hwy), color = "blue") +
labs(title = "Corvettes are a bit of an outlier") +
scale_x_continuous(limits = c(1,8), breaks = c(1:8)) +
geom_text_repel(data=corvette, aes(label=paste("Corvette",year)))
```

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 +111,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(x = class, y = cty, color = class)) +
coord_flip() +
scale_color_brewer(palette = "Set2") +
geom_boxplot() +
geom_jitter(aes(color = class)) +
labs(title = "Horizontal BoxPlot of City MPG and Car Class", x = "Car Class", y = "City mpg") +
theme(panel.border = element_blank(), panel.grid = element_blank()) +
theme(axis.line = element_line(color = "black"))
```


Expand Down