1 line
11 KiB
JSON
1 line
11 KiB
JSON
{"url": "https://mdogucu.ics.uci.edu/teaching/stats67-fa19/slide/slide-2l-intro-viz.html", "content": "<!DOCTYPE html>\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"\" xml:lang=\"\">\r\n <head>\r\n <title>Visualizing Data</title>\r\n <meta charset=\"utf-8\" />\r\n <meta name=\"author\" content=\"Dr.\u00a0Dogucu\" />\r\n <meta name=\"date\" content=\"2019-10-09\" />\r\n <link rel=\"stylesheet\" href=\"slide-styles.css\" type=\"text/css\" />\r\n </head>\r\n <body>\r\n <textarea id=\"source\">\r\nclass: center, middle, inverse, title-slide\r\n\r\n# Visualizing Data\r\n### Dr.\u00a0Dogucu\r\n### 2019-10-09\r\n\r\n---\r\n\r\n\r\nlayout: true\r\n \r\n<div class=\"my-header\"></div>\r\n\r\n<div class=\"my-footer\"> \r\n Copyright &copy; <a href=\"https://mdogucu.ics.uci.edu\">Dr. Mine Dogucu</a>. All Rights Reserved.</div> \r\n\r\n---\r\n\r\n\r\n\r\n\r\n\r\n## Recap \r\n\r\n- Always make a copy of today's project, whenever you start the project for the first time. \r\n\r\n<img src = \"img/cloud-copy.png\" class=\"center-image\"> </img>\r\n\r\n---\r\n\r\n## Recap\r\n\r\n\r\nYou can \"run\" or \"process\" .Rmd file by knitting. To knit your document \r\n\r\n- Clicking the Knit button or\r\n\r\n- Using short cut Ctrl (Cmd on Mac) + Shift + K \r\n\r\nEvery time you knit your .Rmd file is automatically saved.\r\n\r\n---\r\n## Recap\r\n\r\n\r\nWe have learned that we can insert R code chunk to an R Markdown file by \r\n\r\n- Clicking Insert > R or\r\n- using shortcuts Ctrl+Alt+I (Windows) &\tCommand+Option+I (Mac)\r\n\r\n\r\n\r\n---\r\n## One More Short Cut \r\n\r\nFor %>% (aka piper operator) you can use the shortcut Ctrl + Shift + M (Windows) or Cmd + Shift + M (Mac)\r\n\r\nWhen you read out your code, you can read %>% as \"and then\"\r\n\r\n\r\n---\r\n\r\n## Today\r\n\r\nData Visualization using ggplot2 package.\r\n\r\n## Examples\r\n\r\n[BBC](https://bbc.github.io/rcookbook/) \r\n[FiveThirtyEight](https://fivethirtyeight.com/features/the-rise-of-religiously-inspired-terrorism-in-france/) \r\n[Master List](http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html#Histogram) \r\n\r\n---\r\n\r\n\r\n\r\n## Three steps of basic visualization with ggplot2 package\r\n\r\n1. Make some space (this soon will make sense) using the `ggplot()` function.\r\n\r\n2. Draw your axes using `aes()` function.\r\n\r\n3. Add you plot, whether it is histogram, bar plot or something else. We will call these geom objects.\r\n\r\n\r\n---\r\n\r\n## Flow for the Day\r\n\r\nFor every plot, \r\n\r\n1) Decide, what variables to use. \r\n2) Decide which variable is on which axes. \r\n3) Decide what kind of plot it is. \r\n4) Watch the demo for plotting. \r\n---\r\n<br>\r\n<br>\r\n<br>\r\n<img src=\"slide-2l-intro-viz_files/figure-html/unnamed-chunk-2-1.png\" style=\"display: block; margin: auto;\" />\r\n\r\n\r\n???\r\nMake sure to do this step by step please\r\n\r\nShow that titanic %>% ggplot() makes space. \r\ntitanic %>% ggplot(aes(x = Survived)) puts the axes.\r\nand so on.\r\n\r\nNote that they will only see + in R code while using ggplot function because the visualization works in layers and we are adding layers by +.\r\n\r\n\r\n---\r\n\r\n```\r\n## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.\r\n```\r\n\r\n<img src=\"slide-2l-intro-viz_files/figure-html/unnamed-chunk-3-1.png\" style=\"display: block; margin: auto;\" />\r\n\r\nWhy are we getting this warning? \r\n\r\n???\r\n\r\nThey should know what bins are from reading. Remind them. ggplot will set up a default binwidth but it ill also warn you about it. \r\nMake sure to set your own bindwidth. Remind them we had 3 steps of plots 1)\r\nmaking space 2) axes 3) geom object. Which of these steps is bindwidth related to?\r\n---\r\n\r\n<img src=\"slide-2l-intro-viz_files/figure-html/unnamed-chunk-4-1.png\" style=\"display: block; margin: auto;\" />\r\n \r\nWhat step of the code is binwidth related to?\r\n \r\n??? \r\n\r\nSince binwidth is related to the geom object we can put the binwidth argument inside the geom_histogram function. \r\n---\r\n\r\n## Cheatsheet\r\n\r\nLook at the ggplot cheatsheet. Moving on you will attempt at making the plot by finding the geom object from your cheatsheet.\r\n---\r\n<br>\r\n<br>\r\n<br>\r\n<img src=\"slide-2l-intro-viz_files/figure-html/unnamed-chunk-5-1.png\" style=\"display: block; margin: auto;\" />\r\n\r\n---\r\n## You Attempt This First\r\n\r\nMake a plot that shows the relationship between age of the passenger and the fare that they paid. Comment whether older passengers bought more expensive tickets.\r\n\r\n---\r\n\r\n\r\n```\r\n## Warning: Removed 177 rows containing missing values (geom_point).\r\n```\r\n\r\n<img src=\"slide-2l-intro-viz_files/figure-html/unnamed-chunk-6-1.png\" style=\"display: block; margin: auto;\" />\r\n\r\n\r\n---\r\n## Color \r\n\r\n\r\n```r\r\ntitanic_train %>% \r\n ggplot(aes(x = Fare )) + \r\n geom_histogram(color = \"salmon\") \r\n```\r\n\r\n\r\n```\r\n## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.\r\n```\r\n\r\n<!-- -->\r\n\r\n\r\n\r\n---\r\n\r\n## Fill \r\n\r\n\r\n```r\r\ntitanic_train %>% \r\n ggplot(aes(x = Fare)) + \r\n geom_histogram(fill = \"salmon\") \r\n```\r\n\r\n\r\n```\r\n## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.\r\n```\r\n\r\n<!-- -->\r\n\r\n---\r\n\r\n## Shape\r\n\r\n\r\n```r\r\ntitanic_train %>% \r\n ggplot(aes(x = Age, y = Fare )) +\r\n geom_point(shape = 4) \r\n```\r\n\r\n<img src=\"slide-2l-intro-viz_files/figure-html/unnamed-chunk-12-1.png\" style=\"display: block; margin: auto;\" />\r\n---\r\n\r\nclass: center, inverse\r\n## More colors\r\n\r\n<br>\r\n<br>\r\n<br>\r\n\r\n[bit.ly/colors-r](bit.ly/colors-r)\r\n\r\n---\r\n\r\n\r\nclass: center, inverse\r\n## More shapes\r\n\r\n<br>\r\n<br>\r\n<br>\r\n\r\n[bit.ly/shapes-r](bit.ly/shapes-r)\r\n\r\n---\r\n\r\n## Stacked bar plot\r\n\r\nCan you guess where the fill argument will go for this stacked bar plot?\r\n\r\n<img src=\"slide-2l-intro-viz_files/figure-html/unnamed-chunk-13-1.png\" style=\"display: block; margin: auto;\" />\r\n\r\n???\r\n\r\nNote that since it is a barplot y axis is reserved for count\r\nNote that these are the default color scheme of ggplot\r\nThey should focus on adding a second variable rather than changing colors.\r\n\r\n\r\n---\r\n\r\n## Your Turn\r\n\r\nMake this plot with any color other than the default one and submit it. Make sure to set binwidth. `bechdel` is loaded already.\r\n\r\n<img src=\"slide-2l-intro-viz_files/figure-html/unnamed-chunk-14-1.png\" style=\"display: block; margin: auto;\" />\r\n\r\n---\r\n\r\n## Getting Ready for Submission\r\n\r\nAt the top of the .Rmd document you will see a part where my name is written. Replace it with full names of your group members separated by commas. Be careful about the quotation.\r\n\r\nKnit your document as a .pdf one last time. \r\n\r\nOn the files panel, in the lower right, you should see your pdf file. Check the pdf file. Click on More > Export > Download. You now have downloaded your pdf file to your computer.\r\n\r\n\r\n\r\n---\r\n## Submission\r\n\r\n- Log onto Gradescope and upload it. \r\n\r\n- Gradescope may ask you which page has which questions, provide this information.\r\n\r\n- Make sure to add your group members' names\r\n </textarea>\r\n<style data-target=\"print-only\">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>\r\n<script src=\"https://remarkjs.com/downloads/remark-latest.min.js\"></script>\r\n<script>var slideshow = remark.create();\r\nif (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {\r\n window.dispatchEvent(new Event('resize'));\r\n});\r\n(function(d) {\r\n var s = d.createElement(\"style\"), r = d.querySelector(\".remark-slide-scaler\");\r\n if (!r) return;\r\n s.type = \"text/css\"; s.innerHTML = \"@page {size: \" + r.style.width + \" \" + r.style.height +\"; }\";\r\n d.head.appendChild(s);\r\n})(document);\r\n\r\n(function(d) {\r\n var el = d.getElementsByClassName(\"remark-slides-area\");\r\n if (!el) return;\r\n var slide, slides = slideshow.getSlides(), els = el[0].children;\r\n for (var i = 1; i < slides.length; i++) {\r\n slide = slides[i];\r\n if (slide.properties.continued === \"true\" || slide.properties.count === \"false\") {\r\n els[i - 1].className += ' has-continuation';\r\n }\r\n }\r\n var s = d.createElement(\"style\");\r\n s.type = \"text/css\"; s.innerHTML = \"@media print { .has-continuation { display: none; } }\";\r\n d.head.appendChild(s);\r\n})(document);\r\n// delete the temporary CSS (for displaying all slides initially) when the user\r\n// starts to view slides\r\n(function() {\r\n var deleted = false;\r\n slideshow.on('beforeShowSlide', function(slide) {\r\n if (deleted) return;\r\n var sheets = document.styleSheets, node;\r\n for (var i = 0; i < sheets.length; i++) {\r\n node = sheets[i].ownerNode;\r\n if (node.dataset[\"target\"] !== \"print-only\") continue;\r\n node.parentNode.removeChild(node);\r\n }\r\n deleted = true;\r\n });\r\n})();</script>\r\n\r\n<script>\r\n(function() {\r\n var links = document.getElementsByTagName('a');\r\n for (var i = 0; i < links.length; i++) {\r\n if (/^(https?:)?\\/\\//.test(links[i].getAttribute('href'))) {\r\n links[i].target = '_blank';\r\n }\r\n }\r\n})();\r\n</script>\r\n\r\n<script>\r\nslideshow._releaseMath = function(el) {\r\n var i, text, code, codes = el.getElementsByTagName('code');\r\n for (i = 0; i < codes.length;) {\r\n code = codes[i];\r\n if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {\r\n text = code.textContent;\r\n if (/^\\\\\\((.|\\s)+\\\\\\)$/.test(text) || /^\\\\\\[(.|\\s)+\\\\\\]$/.test(text) ||\r\n /^\\$\\$(.|\\s)+\\$\\$$/.test(text) ||\r\n /^\\\\begin\\{([^}]+)\\}(.|\\s)+\\\\end\\{[^}]+\\}$/.test(text)) {\r\n code.outerHTML = code.innerHTML; // remove <code></code>\r\n continue;\r\n }\r\n }\r\n i++;\r\n }\r\n};\r\nslideshow._releaseMath(document);\r\n</script>\r\n<!-- dynamically load mathjax for compatibility with self-contained -->\r\n<script>\r\n(function () {\r\n var script = document.createElement('script');\r\n script.type = 'text/javascript';\r\n script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';\r\n if (location.protocol !== 'file:' && /^https?:/.test(script.src))\r\n script.src = script.src.replace(/^https?:/, '');\r\n document.getElementsByTagName('head')[0].appendChild(script);\r\n})();\r\n</script>\r\n </body>\r\n</html>\r\n", "encoding": "utf-8"} |