Do you want to use R to tell stories? This book was written for you—whether you already know some R or have never coded before.
Most R texts focus only on programming or statistical theory. Practical R for Mass Communication and Journalism gives you ideas, tools, and techniques for incorporating data and visualizations into your narratives.
You’ll see step by step how to:
- Analyze airport flight delays, restaurant inspections, and election results
- Map bank locations, median incomes, and new voting districts
- Compare campaign contributions to final election results
- Extract data from PDFs
- Whip messy data into shape for analysis
- Scrape data from a website
- Create graphics ranging from simple, static charts to interactive visualizations for the Web
If you work or plan to work in a newsroom, government office, non-profit policy organization, or PR office, Practical R for Mass Communication and Journalism will help you use R in your world.
This book has a companion website with code, links to additional resources, and searchable tables by function and task.
Sharon Machlis is the author of Computerworld’s Beginner’s Guide to R, host of InfoWorld’s Do More With R video screencast series, admin for the R for Journalists Google Group, and is well known among Twitter users who follow the #rstats hashtag. She is Director of Editorial Data and Analytics at IDG Communications (parent company of Computerworld, InfoWorld, PC World and Macworld, among others) and a frequent speaker at data journalism and R conferences.
Introduction
Why programming?
Why R?
Is this book for you?
- Get Started With R in a Few Easy Steps
- See How Much You Can Do in a Few Lines of Code
- Import Data into R
- Basic Data Exploration
- Beginning data visualization
- Two or more data sets
- Analyze data by groups
- Graphing by Group
- Write your own R functions
- Maps in R
- Putting it all Together: R on Election Day
- Date calculations
- Help! My data’s in the wrong format!
- Integrate R With Your Storytelling Using R Markdown
- Simple Web scraping
- An R project from start to finish
What we’ll cover
Download R and RStudio
A brief introduction to RStudio
Try out the console
Install packages
Additional infrastructure
Getting help with packages and functions
RStudio keyboard shortcuts
Additional files available online
Wrap-Up
Additional resources
Packages needed this chapter
What we’ll cover
Simple stock market graphing
Download and graph a city’s median income
So many packages!
Running functions without loading packages
Comparing one city’s data to the US median
Run a remote script to make an interactive map
Bonus map: Mapping income data
Wrap-Up
Additional resources
What we’ll cover
Packages needed this chapter
The magic of rio
Import data from packages
What’s a data frame? And what can you do with one?
Easy sample data
Exporting data
Additional resources
Project: Weather data
What we’ll cover
Packages needed this chapter
Download this book’s files
Data summaries
Data ‘interviews’
Slicing and dicing your data set
More sub setting with dplyr
Wrap-Up
Additional resources
Project: More weather data
What we’ll cover: How to
Packages needed this chapter
Answer questions with graphics
Easy visualizations in or lines of code
Some basic graphs
The full power of ggplot
Basic ggplot customizations
Code snippets to the rescue
Presentation-quality graphics
Comment your code
Wrap-up
Additional resources
Project: Multiple files of US airline on-time data
What we’ll cover
Packages needed this chapter
Add one table to the bottom of another
What’s a list, and how do operate on one?
lapply
here () you are!
Wrap-up
Exercise Answer
Additional resources
Project: Airline on-time data analysis (cont)
What we’ll cover
Packages needed this chapter
Lookup tables
Beware of missing values
Bar graph of raw data
Wrap up
Additional resources
Project: Visualizing airline on-time data
What we’ll cover
Packages needed this chapter
Facets
Housing prices by state
Geofacets
Customizing colors
Color palettes
Other packages that extend ggplot functionality
Wrap-up
Additional Resources
Exercise answer
What we’ll cover
Packages needed this chapter
Function basics
seq()
If-then-else
if statements for vectors
A taste of testing
Next steps for your functions
More Resources
Exercise Answer
Exercise Answer
Exercise
Map projects this chapter
Skills we’ll cover
Importing shape files into R
Import data for mapping
An even easier way to pull US Census data
Interactive maps with tmap
Importing and joining data
Leaflet and points on a map
geocoding and R’s paste () function
Time to geocode with R (or maybe without)
Mapping points with leaflet
Points and polygons on a single map
Mapping new political boundaries with leaflet
Inspiration: Washington Post investigation
Wrap-up
Additional resources
Project: Election data
What we’ll cover
Packages needed this chapter
Election Day preparation
Visualizing election results
Graph for a smaller set of results
plotly
Other interactive alternatives
Wrap-up
(Non-election) inspiration
Additional resources
Project: New York City restaurant inspections
What we’ll cover
Packages needed this chapter
Get started with dates in R
Get NYC inspection data
Wrap-up
Inspiration
Additional resources
Project: Election results in a PDF
What we’ll cover
Packages needed this chapter
Human vs machine optimizing
The raw data
Extracting data from PDFs
Tidying the data
Reshaping the data
‘Long’ data back to ‘wide’
Winners and runners-up
Wrap-up
Additional resource
Using tabulizer to unlock the City Council data
Project: Mixing text and R code about that snow data
What we’ll cover
Packages needed this chapter
R Markdown basics
Create an R Markdown document
R Markdown text syntax
R code chunks
Adding R code to run
Add an R-generated graph
Setting option options
Mixing R within text
Even more options
Repeatability with R Markdown parameters
Wrap-up
Additional resources
Project: Download RStudio PDF cheat sheets
What we’ll cover
Packages needed this chapter
Step: Follow the rules with robotstxt
Step: Get a list of links
Step: Download files
Wrap-Up
Additional resources
Project: Local political contribution and election data
What we’ll cover
Packages needed this chapter
Get the data, make it ready for analysis
Standardizing multiple versions of the same name
Making data frames
Analyzing and graphing the results
Visualizing results
Consider R Markdown
Additional resources
Additional resources
More functions, packages and tools worth a look
Stories done with R
Tutorials
Social media, communities, and Web resources
Appendix A Online: How do I
Appendix B Online: Functions
Appendix C Online: Packages
Biography
Sharon Machlis is the author of Computerworld’s Beginner’s Guide to R, host of InfoWorld’s Do More With R video screencast series, admin for the R for Journalists Google Group, and is well known among Twitter users who follow the #rstats hashtag. She is Director of Editorial Data and Analytics at IDG Communications (parent company of Computerworld, InfoWorld, PC World and Macworld, among others) and a frequent speaker at data journalism and R conferences.
"Practical R for Mass Communication and Journalism looks to me like a fabulous resource for those folks who always wanted to learn some more R but were afraid to ask. Definitely recommended." ~Carl Howe, Director of Education, RStudio
"The book can provide a good starting point into working with R. It covers a lot of perspectives that are expected in newsrooms all over the world, especially working with geospatial data. It also provides a lot of good examples and interesting additional resources. The packages used are also mainly part of the standard corpus of R-packages." ~Benedict Witzenberger, Süddeutsche Zeitung
"I am the data editor of a mid-sized newsroom. I have long wished for an Intro to R book that was geared toward journalists, not data scientists. I’ve found that fellow journalists are much more likely to pick up on the intricacies of a computing language like R when they encounter it through a relatable example, like visualizing Election Night votes or analyzing a city council budget. Additionally, there are some R functions that simply aren’t useful for the quantitative needs of most journalists. This is what I appreciated the most about the book – its practical nature (the title doesn’t lie!) Machlis focuses on the concepts that data journalists most frequently encounter and spends little to no time on those they don’t…I also appreciated Chapter 17, "An R Project from Start to Finish." This chapter is exactly why I’ve wanted a journalism-specific Intro to R project that I can recommend to my colleagues" ~Ryann Jones, Deputy Editor, Data at ProPublica
"I like the book. It’s conversationally written, it walks you through common problems in data journalism and for the most part uses the most common libraries to analyze and visualize data…The book’s instructional approach is the real value – it seems aimed at an audience that needs a narrative in order to understand code and analysis. Conveniently, that pretty well describes journalism students and working professionals…I would recommend publication. It advances the field of data journalism and presents a solid text for instructors or practitioners who are interested in R for analysis." ~Matthew Waite
"I NEED THIS BOOK. I may adopt it as a textbook." ~Alberto Cairo, University of Miami
"I’m reading this book now and it is terrific. Highly recommended for anyone interested in learning R. I will be using this book in my Data Analysis for Journalists class in the spring." ~Rob Wells, University of Arkansas
"Sharon Machlis' 'Practical R for Mass Communication and Journalism' is based on the author's workshops for journalists. This book dives straight into doing the kinds of things a busy reporter or news analyst needs to do to meet a 5:00 pm deadline: data cleaning, presentation-quality graphics, and maps take precedence over control flow or the niceties of variable scope. I particularly enjoyed the way each chapter starts with a realistic project and works through what's needed to build it. People who've never programmed before will be a little intimidated by how many packages they need to download if they try to work through the material on their own, but the instructions are clear, and the author's enthusiasm for her material shines through in every example." ~Greg Wilson, RStudio