class: center, middle, inverse, title-slide # Tips from an R Journalist ### Sharon Machlis
Twitter:
@sharon000
### Author, Practical R for Mass Communication and Journalism
These slides:
http://www.machlis.com/eRum2020
--- ```r r_tips_and_ideas %>% group_by( * general_storytelling, * storytelling_with_R, * useful_tips_from_covering_R ) ``` --- ![](img/metrowest_daily_news.jpg) --- ![](img/needy_cat.gif) --- class: center, middle <span style="font-size:80px;">So what? <br /><br /> Who cares? </span> --- ![](img/nyc.png) <span style="font-size:8px;">_Source: [New York Times](https://www.nytimes.com/interactive/2020/04/27/upshot/coronavirus-deaths-new-york-city.html)_ </span> --- ## ggtext [https://wilkelab.org/ggtext/](https://wilkelab.org/ggtext/) ![](img/ggtext.PNG) --- ```r Week <- seq.Date(from = as.Date("2020-02-09"), to = as.Date("2020-04-26"), by = 7) Value <- c(110, -58, 46,- 50, -75, 120, 72, 120, 125, 2560, 3500, 2805) mydf <- data.frame(Week, Value) ggplot(mydf, aes(x = Week, y = Value)) + geom_col() ``` <img src="erum2020_presentation_files/figure-html/unnamed-chunk-2-1.png" width="864" /> --- ```r mydf$Color <- ifelse(mydf$Value > 0, "more", "less") ggplot(mydf, aes(x = Week, y = Value, fill = Color)) + geom_col() + scale_fill_manual(values = c("grey", "firebrick4")) + theme_classic() + theme(legend.position = "none") + xlab("") + ylab("") ``` <img src="erum2020_presentation_files/figure-html/unnamed-chunk-3-1.png" width="864" /> --- ```r library(ggtext) ggplot(mydf, aes(x = Week, y = Value, fill = Color)) + geom_col() + scale_fill_manual(values = c("grey", "firebrick4")) + theme_classic() + theme(legend.position = "none", plot.title = element_markdown()) + xlab("") + ylab("") + labs(title = "<span styl ='font-size:24pt;'>Sample data <span style='color:firebrick4;'>above</span> and <b><span style = 'color:darkgrey;'>below</span></b> normal</span>") ``` <img src="erum2020_presentation_files/figure-html/unnamed-chunk-4-1.png" width="576" /> --- <img src="erum2020_presentation_files/figure-html/unnamed-chunk-5-1.png" width="864" /> --- <img src="erum2020_presentation_files/figure-html/unnamed-chunk-6-1.png" width="864" /> --- ## bbplot package [https://github.com/bbc/bbplot](https://github.com/bbc/bbplot) ![](img/bbplot.PNG) --- ## BBC Visual and Data Journalism Cookbook for R Graphics [https://bbc.github.io/rcookbook/](https://bbc.github.io/rcookbook/) ![](img/bbc_annotation.PNG) --- ```r *windowsFonts(Helvetica = "TT Arial") library(bbplot) ggplot(mydf, aes(x = Week, y = Value, fill = Color)) + geom_col() + bbplot::bbc_style() + scale_fill_manual(values = c("grey", "firebrick4")) + theme(legend.position = "none", plot.title = element_markdown()) + xlab("") + ylab("") + labs(title = "Sample data <span style = 'color:firebrick4;'> above</span> and <b><span style = 'color:darkgrey;'>below</span></b> normal") + geom_label(aes(x = as.Date("2020-03-08"), y = 3030, label = "Many more those last few weeks"), hjust = 0, vjust = 0.5, colour = "#555555", fill = "white", label.size = NA, fontface = "italic", size = 5) + geom_curve(aes(x = as.Date("2020-04-03"), y = 3000, xend = as.Date("2020-04-15"), yend = 3000), colour = "#555555", size=0.5, curvature = -0.2, arrow = arrow(length = unit(0.03, "npc"))) ``` --- ```r windowsFonts(Helvetica = "TT Arial") library(bbplot) ggplot(mydf, aes(x = Week, y = Value, fill = Color)) + geom_col() + * bbplot::bbc_style() + scale_fill_manual(values = c("grey", "firebrick4")) + theme(legend.position = "none", plot.title = element_markdown()) + xlab("") + ylab("") + labs(title = "Sample data <span style = 'color:firebrick4;'> above</span> and <b><span style = 'color:darkgrey;'>below</span></b> normal") + geom_label(aes(x = as.Date("2020-03-08"), y = 3030, label = "Many more those last few weeks"), hjust = 0, vjust = 0.5, colour = "#555555", fill = "white", label.size = NA, fontface = "italic", size = 5) + geom_curve(aes(x = as.Date("2020-04-03"), y = 3000, xend = as.Date("2020-04-15"), yend = 3000), colour = "#555555", size=0.5, curvature = -0.2, arrow = arrow(length = unit(0.03, "npc"))) ``` --- <img src="erum2020_presentation_files/figure-html/unnamed-chunk-9-1.png" width="864" /> --- ![](img/snippets.PNG) --- `usethis::edit_rstudio_snippets()` ![](img/snippets2.PNG) --- Video: <video width="982" height="528" controls> <source src="img/snippets3a.mp4" type="video/mp4"> </video> --- ## [https://bit.ly/RStudio-snippets](https://bit.ly/RStudio-snippets) <iframe width="560" height="315" src="https://www.youtube.com/embed/h_i__VTSurU" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- # Document your code! -- ## roxygen can help (even if you're not creating a package) ![](img/roxygen.PNG) --- <video width="910" height="590" controls> <source src="img/roxygen.mp4" type="video/mp4"> </video> --- ### RStudio function shortcut <video width="910" height="590" controls> <source src="img/extract_function.mp4" type="video/mp4"> </video> --- ### R Markdown Mix text narrative, code, and visualizations <br /> Output to HTML, PDF, Word, and other formats RStudio tutorial: [https://rmarkdown.rstudio.com](https://rmarkdown.rstudio.com/) <br /> RStudio Conference 2020 workshop materials: <br /> [https://github.com/rstudio-conf-2020/rmarkdown-dashboard](https://github.com/rstudio-conf-2020/rmarkdown-dashboard) --- [https://rmarkdown.rstudio.com/lesson-6.html](https://rmarkdown.rstudio.com/lesson-6.html) ![](img/parameters2.png) --- ## Make your own table of packages and functions Useful R packages: [bit.ly/Rpackages](https://bit.ly/Rpackages) How do I? [bit.ly/R_HowDoI](https://bit.ly/R_HowDoI) --- # Interactive tables from dataframes DT [https://rstudio.github.io/DT](https://rstudio.github.io/DT/) <br /> reactable [https://glin.github.io/reactable/](https://glin.github.io/reactable/) ![](img/reactable.PNG) --- ```r DT::datatable(mydf, filter = 'top') ```
--- # Dataviz Resources ## From Data to Viz [https://www.data-to-viz.com](https://www.data-to-viz.com/) ![](img/data2viz.PNG) --- ![](img/data2viz2.PNG) --- ## R Graphs Gallery [https://www.r-graph-gallery.com](https://www.r-graph-gallery.com) ![](img/ragraphgallery3.PNG) --- ## R Graphs Gallery [https://www.r-graph-gallery.com](https://www.r-graph-gallery.com) ![](img/rgraphgallery4.PNG) --- # Dataviz Resources ## The Glamour of Graphics, William Chase Video: [https://rstudio.com/resources/rstudioconf-2020/the-glamour-of-graphics](https://rstudio.com/resources/rstudioconf-2020/the-glamour-of-graphics) Slides: https://www.williamrchase.com/slides/assets/player/KeynoteDHTMLPlayer.html#0 ![](img/glamour.PNG) --- ![](img/willchase.PNG) <br /><br /> _Slide by Will Chase_ --- ### [https://cedricscherer.netlify.app/2019/05/17/the-evolution-of-a-ggplot-ep.-1/](https://cedricscherer.netlify.app/2019/05/17/the-evolution-of-a-ggplot-ep.-1/) ![](img/evol-ggplot-1.gif) --- # Finding Cool New R Things Twitter #rstats hashtag rtweet package by Michael Kearney: [https://rtweet.info/](https://rtweet.info/) My app: [http://apps.machlis.com/shiny/rstats/](http://apps.machlis.com/shiny/rstats/) --- ```r library(rtweet) tweets <- search_tweets("#erum2020", include_rts = FALSE) ``` --- ```r w_urls <- tweets[!is.na(tweets$urls_t.co),] ``` --- ```r library(dplyr) w_urls <- w_urls %>% select(created_at, screen_name, text, url = urls_t.co, favorites = favorite_count, RTs = retweet_count) %>% arrange(desc(favorites)) ``` ![](img/tweets.PNG) --- ### [bit.ly/tweet_R](https://bit.ly/tweet_R) <iframe width="560" height="315" src="https://www.youtube.com/embed/O0gTv9VGRig" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- <svg style="height:0.8em;top:.04em;position:relative;fill:blue;" viewBox="0 0 512 512"><path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"/></svg> [@sharon000](https://twitter.com/sharon000) Do More With R: [https://bit.ly/domorewithR](https://bit.ly/domorewithR) Practical R for Mass Communication and Journalism: [http://machlis.com/R4Journalists/](http://machlis.com/R4Journalists/) These slides: [http://www.machlis.com/eRum2020](http://www.machlis.com/eRum2020)