Introduction

R can be used to make beautiful plots (and tables). The following examples are not very beautiful, but they are functional. They should demonstrate the basic functions and concepts needed to begin making visualizations using R. To make beautiful graphics in R, look at the resources at the bottom of this page.

Tables

Don’t forget that tables are visualizations, too!

Crosstabs Table: Hero/Villian by Male/Female

  1. Use the tabyl function from the janitor package
  2. Use the variables gender and hero_villain

gender

Hero

Villain

Total

female

34

7

41

male

56

53

109

Total

90

60

150

Summary Stats Table

  1. Use describe from the psych package to get summary statistics of each variable in the data set
  2. Use omit=T to remove non-continuous variables
  3. Only display these variables in your table: variable, n, mean, sd, median, min, max

variable

n

mean

sd

median

skew

kurtosis

year_of_release

23

2014.70

3.44

2015.0

-0.48

-1.01

review_rating

23

7.52

0.54

7.5

0.60

-0.44

domestic_gross

23

370607801.74

187972688.97

333176600.0

0.99

0.15

weekend_gross

23

135096585.35

73435307.20

117027503.0

1.25

1.30

overseas_gross

23

610050588.61

404604828.20

473942950.0

1.65

2.85

worldwide_gross

23

980658390.35

576784601.19

853977126.0

1.46

2.09

phase

23

2.22

0.85

2.0

-0.40

-1.56

Color Palette

For today’s examples, we will be using the UTK brand color palette, which can be found here. The colors are reproduced below for quick reference.

Note: To see how this table was made, you can look at the preprocessing.R file.

name

hex

Energy

#EE3E80

Fountain

#2197A9

Leconte

#8D2048

Legacy

#579584

Regalia

#754A7E

Rock

#A7A9AC

Smokey

#58595B

Summitt

#B9E1E2

Tennessee Orange

#FF8200

Valley

#00746F

ggplot

ggplot is a powerful data visualization package that is loaded with the tidyverse. It allows you to build a visualization based on layers, geometries, and aesthetics.

Here is an example. Let’s run it layer by layer. Remove the blue # to run the next layer.

Bars

A Simple Bar Chart

  1. Use geom_bar and stat="count" to make a bar chart to show number of movies for each Marvel series.
  2. Switch between vertical and horizontal bars.
  3. Order the bars using fct_infreq
  4. Reverse the order of the bars by wrapping the previous command in fct_rev
  5. Use fill= to color the bars

Note: Normally, we can reorder a plot based on the y varible using reorder. However, since we only have an x variable (geom_bar only requires 1 variable), we need to use fct_infreq which orders the categories by their frequency.

A Stacked Bar Chart

  1. Use geom_bar to make a bar chart of year_of_release
  2. Use as.factor(year_of_release) to stop R from seeing years as numbers
  3. Use fill=gender to view year of release by gender
  4. Choose two UTK colors to represent females and males
  5. Apply a chart theme with theme_*

Note: We are using as.factor(year_of_release) because R thinks that the variable is numeric (run str(marvel$yer_of_release) in the console to verify. We can set it as a factor here OR we can change it in the data set. Since we will reuse this variable and it is annoying to write as.factor many times, let’s make a change:

Bar Chart with a Quantiative Color Pallete and Labels

  1. This time, add the aesthetics to ggplot()
  2. Use with review_rating and title
  3. geom_col to make a bar chart
  4. Reorder title by review_rating using reorder()
  5. Color the bars by review_rating
  6. Set the low and high colors using the UTK palette Here is a bar chart of IMDB ratings by title.
  7. Use geom_text(aes(label=), hjust=-.5) to add labels at the end

Note: Aesthetics aes() can appear in each geometry, (e.g. geom_col, geom_point) or they can be global in ggplot(). If you will write the same aesthetics again and again, use them in ggplot(). However, if you will be calling different aesthetics, possibly from different data sources, use them in the geometries. I like to use them in the geometries because it helps me visualize in my head as I am building.

Dots

A Scatterplot of Domestic Gross by Rating

  1. Use domestic_gross and review_rating in your aesthetics
  2. Use geom_point() to make a scatterplot
  3. Make all the points one color from the UTK palette
  4. Use color= to map the point color to the rating
  5. Make all points size=3
  6. Use size= to map the point size to the domestic gross
  7. Reuse scale_fill_gradient from the previous plot but not we are using color not fill
  8. Use scale_y_continuous(labels = scales::dollar_format()) to display the domestic gross as dollars.
  9. Use theme(panel.background = element_blank()) to remove the background

Scatterplot of Dichotomous/Categorical Variables

Make a scatterplot of gender and review_rating. Make it easier to see by coloring it by gender.

  1. Use geom_point to make a scatterplot of gender and review_rating
  2. Use geom_jitter instead of geom_point to jitter the categorical variable
  3. Map the point color to gender
  4. Use theme(panel.background = element_blank()) to remove the background
  5. Use panel.grid.major.x = element_line(color="grey90") to add grey gridlines for the x-axis only

Lines

Line Chart of Median Domestic Gross per Year

  1. Use summarize to calculate the median of each year’s domestic gross
  2. Use geom_line() to make a line chart of year and the median
  3. Make the line a UTK color
  4. Add points
  5. Use geom_text(aes(label=label), vjust=-1) to add data labels above the points.

Faceted Line Chart

  1. Make a line chart of domestic_gross by year, grouped and colored by series.
  2. Facet the chart into subcharts by using facet_wrap(~series)
  3. Use gghighlight::gghighlight()
  4. Turn off the labels by adding the argument use_direct_label = F into gghighlight
  5. Organize the faceted chart to have 5 columns. Use ncol=5 in facet_wrap.
  6. Use labeller = label_wrap_gen(width=16)) inside facet_wrap to wrap the panel labels.
  7. Uncomment each line of code, 1 by 1, and run the chart. What changes are being made with each line?
  8. Copy and paste your code into the console. Resize the Plot pane until you are satisfied with the result.

Save a graphic

The easiest way to save a plot is by using “Export” in the plot pane.

Make Your Own

Use the code chunk below to make your own chart!

Resources

Final Steps

Locate the “Knit” button and knit this document to an HTML or Word file. If there are no bugs in your code, it should render in less than 60 seconds!