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.
Don’t forget that tables are visualizations, too!
tabyl function from the janitor packagegender | Hero | Villain | Total |
female | 34 | 7 | 41 |
male | 56 | 53 | 109 |
Total | 90 | 60 | 150 |
describe from the psych package to get summary statistics of each variable in the data setomit=T to remove non-continuous variablesvariable | 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 |
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 |
ggplotggplot 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.
geom_bar and stat="count" to make a bar chart to show number of movies for each Marvel series.fct_infreqfct_revfill= to color the barsNote: Normally, we can reorder a plot based on the y varible using
reorder. However, since we only have an x variable (geom_baronly requires 1 variable), we need to usefct_infreqwhich orders the categories by their frequency.
geom_bar to make a bar chart of year_of_releaseas.factor(year_of_release) to stop R from seeing years as numbersfill=gender to view year of release by gendertheme_*Note: We are using
as.factor(year_of_release)becauseRthinks that the variable is numeric (runstr(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 writeas.factormany times, let’s make a change:
ggplot()geom_col to make a bar chartreorder()geom_text(aes(label=), hjust=-.5) to add labels at the endNote: Aesthetics
aes()can appear in each geometry, (e.g.geom_col,geom_point) or they can be global inggplot(). If you will write the same aesthetics again and again, use them inggplot(). 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.
geom_point() to make a scatterplotcolor= to map the point color to the ratingsize=3size= to map the point size to the domestic grossscale_fill_gradient from the previous plot but not we are using color not fillscale_y_continuous(labels = scales::dollar_format()) to display the domestic gross as dollars.theme(panel.background = element_blank()) to remove the backgroundMake a scatterplot of gender and review_rating. Make it easier to see by coloring it by gender.
geom_point to make a scatterplot of gender and review_ratinggeom_jitter instead of geom_point to jitter the categorical variabletheme(panel.background = element_blank()) to remove the backgroundpanel.grid.major.x = element_line(color="grey90") to add grey gridlines for the x-axis onlysummarize to calculate the median of each year’s domestic grossgeom_line() to make a line chart of year and the mediangeom_text(aes(label=label), vjust=-1) to add data labels above the points.facet_wrap(~series)gghighlight::gghighlight()use_direct_label = F into gghighlightncol=5 in facet_wrap.labeller = label_wrap_gen(width=16)) inside facet_wrap to wrap the panel labels.The easiest way to save a plot is by using “Export” in the plot pane.
Use the code chunk below to make your own chart!
Check out #TidyTuesday to see what can be made with R and ggplot: https://nsgrantham.shinyapps.io/tidytuesdayrocks/
See examples and follow along with the R Graph Gallery: http://r-graph-gallery.com/
Modify components of a theme: https://ggplot2.tidyverse.org/reference/theme.html
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!