diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/CityMPGvsCarClass_Plot.png b/CityMPGvsCarClass_Plot.png new file mode 100644 index 0000000..15ced3f Binary files /dev/null and b/CityMPGvsCarClass_Plot.png differ diff --git a/Corvette_plot.png b/Corvette_plot.png new file mode 100644 index 0000000..205ce97 Binary files /dev/null and b/Corvette_plot.png differ diff --git a/Diaomonds_Plot.png b/Diaomonds_Plot.png new file mode 100644 index 0000000..8c8206f Binary files /dev/null and b/Diaomonds_Plot.png differ diff --git a/HW02_A_Graph-Fails.Rmd b/HW02_A_Graph-Fails.Rmd index eb8937c..6f98b03 100644 --- a/HW02_A_Graph-Fails.Rmd +++ b/HW02_A_Graph-Fails.Rmd @@ -33,7 +33,7 @@ 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() ``` @@ -41,8 +41,9 @@ mpg %>% ### 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")) +mpg %>% + ggplot(mapping = aes(x = displ, y = hwy)) + + geom_point(color = 'blue') ``` @@ -55,9 +56,12 @@ 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.title = element_blank()) theme(legend.direction = "horizontal") + - theme(legend.position = c(5, 40)) + theme(legend.position = c(.714, .8)) + + #to remove the legend, use: theme(legend.position='none') ``` @@ -66,8 +70,8 @@ I wanted just one smoothing line. Just one line, to show the general relationshi ```{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 ``` @@ -79,9 +83,7 @@ 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)) + - geom_boxplot() + - theme(axis.text.x = element_text(angle = 45)) -``` - - - + geom_boxplot(aes(fill = manufacturer, alpha =0.1)) + + theme(axis.text.x = element_text(angle = 45)) + + theme(axis.text.x = element_text(margin = margin(t = 20))) +``` \ No newline at end of file diff --git a/HW02_B_Mimic.md b/HW02_B_Mimic.md index 28df272..04f93bd 100644 --- a/HW02_B_Mimic.md +++ b/HW02_B_Mimic.md @@ -32,6 +32,17 @@ is for. :smile: ``` r data("diamonds") #hint think about the *position* the bars are in... + +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", x = "Diamond Cut", y = "Number of Diamonds") + + annotate( + "text", + x = 4, y = 5000, + label = "My Best Diamonds, of course", + vjust = 1, size = 3 + ) + ``` Using the diamonds dataset, make this graph: @@ -46,6 +57,11 @@ data("iris") Using the iris dataset, make this graph: ## `geom_smooth()` using formula 'y ~ x' + +ggplot(iris, aes(Sepal.Length, Petal.Length)) + + geom_point(aes(shape = factor(Species), color = factor(Species))) + + facet_wrap("Species") + + geom_smooth(formula = y ~ x) ![](HW02_B_Mimic_files/figure-gfm/unnamed-chunk-4-1.png) @@ -69,6 +85,12 @@ 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. +ggplot(mpg, aes(displ, hwy)) + + geom_point() + + geom_point(data = corvette, color = "red") + + geom_text(data = corvette, aes(label = paste(model, year), hjust = 1.1)) + + labs(title = "Corvettes are a bit of an outlier") + ### Graph 4 ``` r @@ -87,3 +109,11 @@ graph below, I used Set2. Now using the above mpg dataset, make this graph ![](HW02_B_Mimic_files/figure-gfm/unnamed-chunk-8-1.png) + +ggplot(mpg, aes(class, cty, color = class)) + + geom_boxplot() + + geom_jitter() + + coord_flip() + + scale_color_brewer(palette="Set2") + + labs(title = "Horizontal Boxplot of City MPG and Car Class", y = "City MPG", x = "Car Class") + diff --git a/Homework2.Rproj b/Homework2.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/Homework2.Rproj @@ -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 diff --git a/Iris_Plot.png b/Iris_Plot.png new file mode 100644 index 0000000..b715b2b Binary files /dev/null and b/Iris_Plot.png differ diff --git a/README.md b/README.md index 241da2d..0f9a627 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,11 @@ 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 +#I have attempted to fix the code in 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" +#I have written code for the graphs in HW02_B_Mimic.md +#My PNG files should be included in this directory for the graphs. + +#Thanks for stopping by! \ No newline at end of file