Data Analysis With R Programming
Data Analysis With R Programming
Provides an
What is a Includes a variety of Allows users to accessible
primary visualization tools manipulate and language to
advantage? and features reorganize data as organize, modify,
and clean data
Key question Spreadsheets SQL R
Which datasets
does it work Smaller datasets Larger datasets Larger datasets
best with?
Loaded with R
when installed,
What is the Entered manually or
Accessed from an imported from
source of the imported from an
external database your computer, or
data? external source
loaded from
external sources
Where is the
data from my In a spreadsheet file Inside tables in the In an R file on your
analysis usually on your computer accessed database computer
stored?
Do I use
formulas and Yes Yes Yes
functions?
Yes, by using an
additional tool like
a database
Can I create management
Yes Yes
visualizations? system (DBMS) or
a business
intelligence (BI)
tool
Introduction to R
R: A programming language frequently used for statistical analysis, visualization, and other
data analysis. R is based on S. The title R refers to the first names of its two authors and
plays on a single- letter tittle of its predecessor S. The R programming language can be used
for statistical analysis, visualization, and data analysis.
Why R is popular?
• Accessible First R is an accessible language for beginners.
• Data-centric It's specifically designed to make data analysis easier, more efficient and
more powerful.
• Open source Open source: Code that is freely available and may be modified and
shared by the people who use it. Thousands of R packages exists.
• Community
o RStudio Community: The RStudio Community forum is a great place to get
help and find solutions to challenges you have with R–and maybe help
someone else out, too!
o r/RLanguage: The R language subreddit is an active online community on the
social media platform Reddit, where R users go to discuss R, ask questions,
and share tips.
o rOpenSci: rOpenSci has a community forum where R users can ask questions
and search for solutions. It also includes links to their Best Practices guide and
support pages.
o R4DS Online Learning Community and Slack channel: This is a community
with another Slack channel where R learners and mentors can gather and
connect. This is a great place to chat about using R for data science.
o Twitter #rstats: If you use Twitter, you can connect with other R users using
the hashtag #rstats; a lot of R developers and analysts are active on Twitter.
Meetups
o Local Data Analytics meetups: These meetups are a great way to meet other
people who are interested in data analytics and build your network. These
meetups are location-based, so you can connect with other data analysts in
your area.
o R User Groups: This list contains links to regional R communities, including
subreddits and meetup groups. This is a useful resource if you are interested
in finding R users in your area.
o RLadies Meetups: These are in-person and virtual meetups specifically for R
enthusiasts who identify as underrepresented or marginalized. These
meetups are also location-based and can help you connect with other data
analysts in your area.
Three specific scenarios situations when you might use R for data analysis:
• Reproducing your analysis R can save and reproduce every step of your analysis. Your
code stores it forever. And you can share it with anyone at any time.
• Processing lots of data Processing lots of data is also something R does really well,
just like SQL.
• Creating data visualizations R can create powerful visuals and has state-of-the-art
graphic capabilities. If you work with more advanced packages, you can make some
seriously impressive data visualizations.
When to use RStudio
R and RStudio are designed to handle large data sets, which spreadsheets might not be able
to handle as well. RStudio also makes it easy to reproduce your work on different datasets.
When you input your code, it's simple to just load a new dataset and run your scripts again.
You can also create more detailed visualizations using RStudio.
When the data is spread across multiple categories or groups, it can be challenging to
manage your analysis, visualize trends, and build graphics. That’s where RStudio comes in.
For example, imagine you are analyzing sales data for every city across an entire country.
That is a lot of data from a lot of different groups–in this case, each city has its own group of
data.
Here are a few ways RStudio could help in this situation:
• Using RStudio makes it easy to take a specific analysis step and perform it for each
group using basic code. In this example, you could calculate the yearly average sales
data for every city.
• RStudio also allows for flexible data visualization. You can visualize differences across
the cities effectively using plotting features like facets–which you’ll learn more about
later on.
• You can also use RStudio to automatically create an output of summary stats—or
even your visualized plots—for each group.
For more information
• The Advantages of RStudio: This web page explains some of the reasons why
RStudio is many analysts’ preferred choice for interfacing with R. You’ll learn about
the advantages of using RStudio for data analysis, from ease of use to accessibility of
graphics and more.
• Data analysis and R programming: This online introduction to data analysis and R
programming is a good starting point for R and RStudio users. It also includes a list of
detailed explanations about the advantages of using R and RStudio. You’ll also find a
helpful guide for getting set up with RStudio.
RStudio
Integrated Development Environment (IDE): A software application that brings together all
the tools you may want to use in a single place. RStudio is an IDE.
RStudio includes R console, and also includes an editor for writing code, and tools for
managing your data and creating visuals.
# Good # Bad
Function names should be verbs.
add () addition ()
Syntax
# Bad
Always put a space after a # Good
y[,2]
comma (never before). y[, 2]
y[ ,2]
Examples of best Examples
Guidance
practice to avoid
# Bad
x <- 7
if (x > 0)
# Good {
An opening curly brace should
x <- 7 print("x is
never go on its own line and
if (x > 0) { apositive
should always be followed by a
print("x is a positive number")
Curly new line. A closing curly brace
number")} else { }
braces should always go on its own line
print ("x is either else {
(unless it’s followed by an else
anegative number print ("x is
statement). Always indent the
or zero") either
code inside curly braces.
} anegative
number
orzero")
}
# Good # Bad
Assignment Use <- , not = , for assignment.
z <- 4 Z=4
Organization
Examples of Examples to
Guidance
best practice avoid
Additional Resources
• Check out this tidyverse style guide to get a more comprehensive breakdown of the
most important stylistic conventions for writing R code (and working with the
tidyverse).
• The styler package is an automatic styling tool that follows the tidyverse formatting
rules. Check out the styler webpage to learn more about the basic features of this
tool.
Debugging Resources
• For more information on the technical aspects of debugging R code, check
out Debugging with RStudio on the RStudio Support website. RStudio Support is a
great place to find answers to your questions about RStudio. This article will take you
through the R debugging tools built into RStudio, and show you how to use them to
help debug R code.
• To learn more about problem-solving strategies for debugging R code, check out the
chapter on Debugging in Advanced R. Advanced R is a great resource if you want to
explore the finer details of an R topic and take your knowledge to the next level.
Hands-On Activity: R sandbox
In this activity, you will be using a package called tidyverse. The tidyverse package is actually
a collection individual packages that can help you perform a wide variety of analysis tasks.
[Link]("tidyverse")
library(tidyverse)
Many of the tidyverse packages contain sample datasets that you can use to practice
your R skills. The diamonds dataset in the ggplot2 package is a great example for
previewing R functions.
Preview
One common function you can use to preview the data is the head() function, which displays the
columns and the first several rows of data
The str() and glimpse() functions will both return summaries of each column in your data arranged
horizontally colnames() function returns a list of column names from your dataset
The numbers [1],[10] helps you count the number of columns in your dataset summarize(). can be
Renaming
diamond:
Separate out some of the components by creating a different plot for each type of cut with
Tidyverse workflow:
tidyverse core
8 core tidyverse packages:
• ggplot2 Ggplot2 is used for data visualization, specifically plots. With ggplot2, you
can create a variety of data viz by applying different visual properties to the data
variables.
• tidyr tidyr (R): A package used for data cleaning to make tidy data. tidy(or clean)
data: data where every part of a data table or data frame is the right type in the right
place.
• readr Used for importing data. Most common function is read_csv() To accurately
read a dataset with readr, you can combine the function with a column
specification(describes how each column should be converted to the most
appropriate data type). (Not necessary because readr will figure it out automatically.)
• dplyr dplyr (R): Offers a consistent set of functions that help you complete some
common data manipulation tasks. For example, the select function picks variables
based on their names, and the filter function finds cases where certain conditions are
true.
(↑ Above four packages that are an essential part of the workflow for data analysts ↑)
• tibble Tibble works with data frames.
• purrr Purrr works with functions and vectors helping make your code easier to write
and more expressive.
• stringr Stringr includes functions that make it easier to work with strings.
• forcats Forcats provides tools that solve common problems with factors. Factors (R):
Store categorical data in R where the data values are limited and usually based on a
finite group like country or year.
Update
# update all of your packages:
[Link]()
vignette
A vignette is documentation that acts as a guide to an R package.
The browseVignettes function allows you to read through vignettes of a loaded package.
browseVignettes()
browseVignettes("packagename")
# e.g.
browseVignettes("ggplot2")
If you are using RStudio Cloud, running this function will open a new browser tab with
links to the vignettes.
Pip
Pipe (R): A tool in R for expressing a sequence of multiple operations, represented with
“%>%". In other words, it takes the output of one statement and makes it the input of the
next statement. So instead of typing out functions contained inside other functions, you
could use the pipe operator to do the same work. In programming, we describe this as
nested.
Nested: In programming, describes code that performs a particular function and is contained
within code that performs a broader function. Nested function: A function that is
completely contained within another function.
You can think of a pipe as a way to code the phrase and then.
e.g. find the mean or average of the sales data:
• Call up data (and then)
• Group the data (and then)
• Summarize the grouped data using a mean function
data("ToothGrowth")
View(ToothGrowth)
# Nested function
arrange(filter(ToothGrowth, dose==0.5), len)
tribble(
~x, ~y, ~z,
"a", 2, 3.6,
"b", 1, 8.5
)
The tibble only returns the first 10 rows in a neatly organized table:
• The entry for Tibble in the tidyverse documentation summarizes what a tibble is and
how it works in R code. If you want a quick overview of the essentials, this is the
place to go.
• The [Tidy chapter]([Link]
cookbook/[Link]# "This link takes you to the Tidy chapter in "A Tidyverse
Cookbook." ") in "A Tidyverse Cookbook" is a great resource if you want to learn
more about how to work with tibbles using R code. The chapter explores a variety of
R functions that can help you create and transform tibbles to organize and tidy your
data.
Warning: tibble will lost row names!
rownames(mtcars)
# [1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" ...
rownames(as_tibble(mtcars))
# [1] "1" "2" "3" "4" ...
Working with data frames
Data frames are basically the data analyst's default way to interact with data.
'diamonds' is a good dataset to practice with.
[Link]('tidyverse')
library('tidyverse')
dataset('diamonds') # 'diamonds' dataset is in 'ggplot2'
View(diamonds)
# structure
str(diamonds)
glimpse(diamonds)
# create a two-column-data-frame
people <- [Link](names, age)
# another example
Data_Frame <- [Link] (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Load dataset from packages
# display data sets in package 'datasets'
data()
# load a dataset
data(mtcars)
The loaded dataset will also appear in the Environment pane of your RStudio. Click directly
on the name of the dataset in the Environment pane, or using mtcars to display the dataset.
Readr from files like csv
The readr package in R is a great tool for reading rectangular data. Rectangular data is data
that fits nicely inside a rectangle of rows and columns, with each column referring to a single
variable and each row referring to a single observation.
Here are some examples of file types that store rectangular data:
• .csv (comma separated values): a .csv file is a plain text file that contains a list of
data. They mostly use commas to separate (or delimit) data, but sometimes they use
other characters, like semicolons.
• .tsv (tab separated values): a .tsv file stores a data table in which the columns of data
are separated by tabs. For example, a database table or spreadsheet data.
• .fwf (fixed width files): a .fwf file has a specific format that allows for the saving of
textual data in an organized fashion.
• .log: a .log file is a computer-generated file that records events from operating
systems and other software programs.
readr functions:
• read_csv(): comma-separated values (.csv) files
• read_tsv(): tab-separated values files
• read_delim(): general delimited files
• read_fwf(): fixed-width files
• read_table(): tabular files where columns are separated by white-space
• read_log(): web log files
Reading a .csv file with readr
library(tidyverse)
# read file
# Prints out a column specification and a tibble.
> read_csv(readr_example("[Link]"))
── Column specification
───────────────────────────────────────────────────────────────────────────
──────
cols(
mpg = col_double(),
cyl = col_double(),
disp = col_double(),
hp = col_double(),
drat = col_double(),
wt = col_double(),
qsec = col_double(),
vs = col_double(),
am = col_double(),
gear = col_double(),
carb = col_double()
)
# A tibble: 32 x 11
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
# … with 22 more rows
Readxl from Excel files .xlsx
library(readxl)
# read an example (by default read the first tab) and returns a tibble
> read_excel(readxl_example("[Link]"))
# A tibble: 10 x 2
`maybe boolean?` description
<chr> <chr>
1 NA "empty"
20 "0 (numeric)"
31 "1 (numeric)"
4 40908 "datetime"
5 TRUE "boolean true"
6 FALSE "boolean false"
7 cabbage "\"cabbage\""
8 true "the string \"true\""
9F "the letter \"F\""
10 False "\"False\" preceded by single quote"
[Link]("janitor")
library("janitor")
library("dplyr")
The palmer penguin data: has lots of information about three penguin species in the Palmer
Archipelago, including size measurements, clutch sizes, and blood isotope ratios.
[Link]("palmerpenguins")
library("palmerpenguins")
Functions that useful in data cleaning:
skim_without_charts()
glimpse()
head()
select()
rename()
clean_names()
skim_without_charts()
glimpse()
head()
select()
# only select column species
penguins %>%
select(species)
# to lower case
rename_with(penguins, tolower)
clean_names() Cleans names of an object (usually a [Link]). Resulting names are
unique and consist only of the _character, numbers, and letters. Capitalization preferences
can be specified using the case parameter. clean_names(penguins)
File-naming conventions
An important part of cleaning data is making sure that all of your files are accurately named.
Next are some helpful "do’s" and "don’ts" to keep in mind when naming your files.
Do
• Keep your filenames to a reasonable length
• Use underscores and hyphens for readability
• Start or end your filename with a letter or number
• Use a standard date format when applicable; example: YYYY-MM-DD
• Use filenames for related files that work well with default ordering; example: in
chronological order, or logical order using numbers first
Examples of good filenames
2020-04-10_march-attendance.R
2021_03_20_new_customer_ids.csv
01_data-[Link]
02_data-[Link]
Don't
• Use unnecessary additional characters in filenames
• Use spaces or “illegal” characters; examples: &, %, #, <, or >
• Start or end your filename with a symbol
• Use incomplete or inconsistent date formats; example: M-D-YY
• Use filenames for related files that do not work well with default ordering; examples:
a random system of numbers or date formats, or using letters first
4102020marchattendance<workinprogress>.R
_20210320*[Link]
firstfile_for_datasales/[Link]
secondfile_for_datasales/[Link]
Additional resources
These resources include more info about some of the file naming standards discussed here,
and provide additional insights into best practices.
• How to name files: this resource from Speaker Deck is a playful take on file naming.
It includes several slides with tips and examples for how to accurately name lots of
different types of files. You will learn why filenames should be both machine
readable and human readable.
• File naming and structure: this resource from the Princeton University Library
provides an easy-to-scan list of best practices, considerations, and examples for
developing file naming conventions.
Organize data
arrange()
group_by()
filter()
Sort
# sort penguins with bill_length_mm in ascending order
penguins %>%
arrange(bill_length_mm)
# in descending order
penguins %>%
arrange(-bill_length_mm)
# or
penguins %>%
arrange(desc(bill_length_mm))
Group by
group_by is usually combined with other functions. For example, we might want
to group_by a certain column and then perform an operation on those groups. we
can group_by island and then use the summarize function to get the mean bill length. The
summarize function lets us get high-level information about our penguin data. You can use
max,min,mean,sum, etc.. insdie the summarize funciton.
penguins %>%
group_by(island) %>%
drop_na() %>%
summarize(mean_bill_length_mm = mean(bill_length_mm))
>
# A tibble: 3 x 2
island mean_bill_length_mm
<fct> <dbl>
1 Biscoe 45.2
2 Dream 44.2
3 Torgersen 39.0
penguins %>%
group_by(species, island) %>%
drop_na() %>%
summarize(max_bl=max(bill_length_mm), mean_bl=mean(bill_length_mm))
>
`summarise()` has grouped output by 'species'. You can override using the `.groups`
argument.
# A tibble: 5 x 4
# Groups: species [3]
species island max_bl mean_bl
<fct> <fct> <dbl> <dbl>
1 Adelie Biscoe 45.6 39.0
2 Adelie Dream 44.1 38.5
3 Adelie Torgersen 46 39.0
4 Chinstrap Dream 58 48.8
5 Gentoo Biscoe 59.6 47.6
Be careful when you're using drop_na. It's useful when doing a group-level summary
statistic, but it will remove rows from the data.
Filter
penguins %>%
filter(species == "Adelie")
penguins %>%
mutate(body_mass_kg = body_mass_g/1000, flipper_length_m =
flipper_length_mm/1000)
Wide to long with tidyr
Wide data has observations across several columns. Same data in a long format:
from wide data to long data pivot_longer() As part of the tidyr package, you can use this R
function to lengthen the data in a data frame by increasing the number of rows and
decreasing the number of columns.
billboard %>%
pivot_longer(
cols = starts_with("wk"),
names_to = "week",
names_prefix = "wk",
values_to = "rank",
values_drop_na = TRUE
)
from long data to wide data Similarly, if you want to convert your data to have more
columns and fewer rows, you would use the pivot_wider() function.
us_rent_income %>%
pivot_wider(
names_from = variable,
names_sep = ".",
values_from = c(estimate, moe)
)
Additional resources To learn more about these two functions and how to apply them in
your R programming, check out these resources:
• Pivoting: Consider this a starting point for tidying data through wide and long
conversions. This web page is taken directly from tidyr package information
at [Link]. It explores the components of the pivot_longer and pivot_wider
functions using specific details, examples, and definitions.
• CleanItUp 5: R-Ladies Sydney: Wide to Long to Wide to…PIVOT: This resource gives
you additional details about the pivot_longer and pivot_wider functions. The
examples provided use interesting datasets to illustrate how to convert data from
wide to long and back to wide.
• Plotting multiple variables: This resource explains how to visualize wide and long
data, with ggplot2 to help tidy it. The focus is on using pivot_longer to restructure
data and make similar plots of a number of variables at once. You can apply what you
learn from the other resources here for a broader understanding of the pivot
functions.
A closer look
Misleading statistic
a very famous data example, Anscombe's Quartet. Anscombe's quartet: Four datasets that
have nearly identical summary statistics. However, those summary statistics might be
misleading.
The cor() function returns the correlation between two variables. This determines how
strong the relationship between those two variables is.
[Link]('Tmisc')
library(Tmisc)
data("quartet")
View(quartet)
skim_without_charts(quartet)
If we had just gone with the statistical summaries, we never would have known that
this data is actually really different.
Draw different shapes
The data source creates plots with the Anscombe data in different shapes.´
[Link]('datasauRus')
library('datasauRus')
Bias
Every data analyst will encounter an element of bias at some point in the data analysis
process. That’s why it’s so important to understand how to identify and manage biased data
whenever possible.
In R, we can actually quantify bias by comparing the actual outcome of our data with the
predicted outcome using bias() function. The bias() function can be used to calculate the
average amount a predicted outcome and actual outcome differ in order to determine if the
data model is biased.
If the model is unbiased, the outcome should be pretty close to zero. A high result means
that your data might be biased.
e.g. 1 Determine if a local weather channel's weather predictions are biased. We'll use the
bias function to compare forecasted temperatures with actual temperatures.
[Link]('SimDesign')
library('SimDesign')
bias(actual_temp, predicted_temp)
We can find out that the result is 0.71. That's pretty high. The predictions seem biased
towards lower temperatures, which means they aren't as accurate as they could be.
e.g. 2 One of the tasks is called a side-by-side comparison. For example, we might show
users two ads side-by-side at the same time. In our survey, we ask which of the two ads they
prefer. In one case, after many iterations, we were seeing consistent bias in favor of the first
item. There was also a measurable decrease in the preference for an item if we swapped its
position to second.
We used sample() to inject a randomization element into our R programming. In R, the
sample() function allows you to take a random sample of elements from a data set.
x <- 1:12
# a random permutation
sample(x)
the size, shape, color, or location (x-axis, y-axis) of your data points.
• Geoms Geom $(\mathrm{R})$: The geometric object used to represent your data.
For example, you can use points to create a scatter plot, bars to create a bar chart, or
lines to create a line diagram. Points show the relationship between two quantitative
variables. Bars show one quantitative variable varies across different categories.
• Facets Facets $(\mathrm{R})$: Let you display smaller groups, or subsets, of your
data. With facets, you can create separate plots for all the variables in your dataset.
• Labels and annotations Labels and annotations $(\mathrm{R})$: Let you customize
your plot. You can add text like titles, subtitles and captions to communicate the
purpose of your plot or highlight important data.
Hands-on Activity
Plot the relationship between body mass and flipper length in the three penguin species.
Load libraries and dataset:
[Link]("ggplot2")
[Link]("palmerpenguins")
library(ggplot2)
library(palmerpenguins)
Full Template
Cheat Sheet
Explore aesthetics
Aesthetic (R): A visual property of an object in your plot. For example, in a scatter plot,
aesthetics include the size, shape or color of your data points.
Aesthetics for points:
• X
• Y
• Color this allows you to change the color of all of the points on your plot, or the color
of each data group.
• Shape this allows you to change the shape of the points on your plot by data group.
• Size this allows you to change the size of the points on your plot by data group.
• Alpha
Map aesthetic color to another variable "species":
ggplot(data = penguins) +
geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color=species))
We can also map the variable species to the aesthetic shape by
changing color=species to shape=species.
We can map more than one aesthetic to the same variable: ..., color=species, shape=species,
size=species, alpha=species)) Eachr colored shape will also be a different size, while alpha
Same data using different geom: (Left uses geom_point. Right uses a geom_smooth.)
Geom (R): The geometrical object used to represent your data.
Geom functions:
• geom_point
• geom_bar
• geom_line
• etc.
geom_smooth
Use two geoms in the same plot:
ggplot(data = penguins) +
geom_smooth(mapping = aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
Gam smoothing, or
generalized additive ggplot(data, aes(x=, y=)) +
model smoothing, is geom_point() +
Gam smoothing
useful for smoothing geom_smooth(method="gam",
plots with a large formula = y ~s(x))
number of points.
By default, method
is chosen
automatically based
on the size of the
largest group.
geom_jitter()
The geom_jitter() function creates a scatter plot and then adds a small amount of random
noise to each point in the plot. Jittering helps us deal with over-plotting, which happens
when the data points in a plot overlap with each other. ggplot(data = penguins) +
geom_jitter(mapping = aes(x = flipper_length_mm, y = body_mass_g))
theme()
Rotates the text to 45 degrees to make it easier to read: + theme([Link].x =
element_text(angle = 45))
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = distribution_channel)) +
facet_wrap(~deposit_type) +
theme([Link].x = element_text(angle = 45))
geom_bar()
When you use geom underscore bar, R automatically counts how many times each x-value
appears in the data, and then shows the counts on the y-axis. The default for geom
underscore bar is to count rows.
Some aesthetics:
• color: outline color of the bars
• fill: inside color of the bars
ggplot(data = diamonds) +
geom_bar(mapping = aes(x=cut))
ggplot(data = diamonds) +
geom_bar(mapping = aes(x=cut, fill=cut))
If we map fill to a new variable, geom_bar() will display what's called a stacked bar chart.
Let's map fill to clarity instead of cut.
ggplot(data = diamonds) +
geom_bar(mapping = aes(x=cut, fill=clarity))
Our plot now shows 40 different combinations of cut and clarity. Each combination has its
facet_grid
To facet your plot with two variables, use the facet_grid() function. Facet underscore grid
will split the plot into facets vertically by the values of the first variable and horizontally by
the values of the second variable.
ggplot(data = penguins) +
geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color=species)) +
facet_grid(sex~species)
# or facet_grid(~sex~species)
If we want, we can focus our plot on only one of the two variables:
ggplot(data = penguins) +
geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color=species)) +
facet_grid(~sex) +
theme([Link].x = element_text(angle = 45))
filtering data for plotting
Use the filter function from dplyr to make the plots you create with ggplot2 easier to read:
data %>%
filter(variable1 == "DS") %>%
ggplot(aes(x = weight, y = variable2, colour = variable1)) +
geom_point(alpha = 0.3, position = position_jitter()) + stat_smooth(method = "lm")
# Lesson4_Annotations.Rmd
ggplot(data = hotel_bookings) +
geom_bar(mapping = aes(x = market_segment)) +
facet_wrap(~hotel) +
theme([Link].x = element_text(angle = 45)) +
labs(title="Comparison of market segments by hotel type for hotel bookings",
caption=paste0("Data from: ", mindate, " to ", maxdate),
x="Market Segment",
y="Number of Bookings")
annotate function
If we want to put text inside the grid to call out specific data points, we can use the annotate
function. annotate("text", x=220, y=3500, label="The Gentoos are the largest") <type of
label> <the specific location of the label> <the context of the label>.
We can change text color, font style, size or even angle: annotate("text", x=220, y=3500,
# Add segments
p + annotate("segment", x = 1, xend = 3, y = 25, yend = 15, colour = "purple", size=3,
alpha=0.6)
# Add arrow
p + annotate("segment", x = 2, xend = 4, y = 15, yend = 25, colour = "pink", size=3, alpha=0.6,
arrow=arrow())
• Create an annotation layer: This guide explains how to add an annotation layer with
ggplot2. It includes sample code and data visualizations with annotations created in
ggplot2.
• How to annotate a plot in ggplot2: This resource includes explanations about how to
add different kinds of annotations to your ggplot2 plots, and is a great reference if
you need to quickly look up a specific kind of annotation.
• Annotations: Chapter eight of the online ggplot2 textbook is focused entirely on
annotations. It provides in-depth explanations of the different types of annotations,
how they are used, and detailed examples.
• How to annotate a plot: This R-Bloggers article includes explanations about how to
annotate plots in ggplot2. It starts with basic concepts and covers more complicated
information the further on you read.
• Text Annotations: This resource focuses specifically on adding text annotations and
labels to ggplot2 visualizations.
Saving your visualizations
Use the Export option in the plots tab of RStudio or the ggsave function provided by the
ggplot2 package.
Export
ggsave
It defaults to saving the last plot that you displayed and uses the size of the current graphics
device.
# save png file at `getwd()` location
ggsave("Three Penguin [Link]")
Change your working directory
• Use "Session --> Set Working Directory" to set the work directory. Or,
• getwd(), setwd("/path/to/my/directory")
specify the height and width
• ggsave('[Link]', width=16, height=8)
Saving images without ggsave
To save images without using ggsave(), you can open a regular R graphics device
like png() or pdf(); these will allow you to save your plot as a .png or .pdf file. You can also
choose to print the plot and then close the device using [Link]().
png(file =
"[Link]", bg = pdf(file = "/Users/username/Desktop/[Link]",
"transparent") plot(1:10) width = 4, height = 4) plot(x = 1:10, y = 1:10) abline(v =
rect(1, 5, 3, 7, col = "white") 0) text(x = 0, y = 1, labels = "Random text") [Link]()
[Link]()
• Saving images without ggsave(): This resource is pulled directly from the ggplot2
documentation at [Link]. It explores the tools you can use to save images in
R, and includes several examples to follow along with and learn how to save images
in your own R workspace.
• How to save a ggplot: This resource covers multiple different methods for saving
ggplots. It also includes copyable code with explanations about how each function is
being used so that you can better understand each step in the process.
• Saving a plot in R: This guide covers multiple file formats that you can use to save
your plots in R. Each section includes an example with an actual plot that you can
copy and use for practice in your own R workspace.
Documentation and reports
Document and report your work using R Markdown.
You can use an R Markdown file as a code notebook to save, organize, and document your
analysis using code chunks, comments, and other features.
An overview of R Markdown How to install R Markdown in RStudio How to create an R
Markdown document The structure and components of the document How to insert and
edit pieces of code called chunks in your document The process of exporting your
documentation
Overview of R Markdown
R Markdown: A file format for making dynamic documents with R. It ties together your code
and your report so you can share every step of your analysis. And you don't even have to
leave RStudio to do this.
R Markdown documents are written in Markdown. Markdown: A syntax for formatting plain
text files.
Besides text, R Markdown also includes an interactive option called an R Notebook that lets
users run your code and show the graphs and charts that visualize the code. R Notebook:
Lets users run your code and show the graphs and charts that visualize the code.
R Markdown lets you convert your files into lots of different formats:
• HTML, PDF, and Word documents
• Slide presentation
• Dashboard
The Markdown language was originally designed for HTML output. HTML: The set of markup
symbols or codes used to create a webpage.
File > New File > R Markdown > From Template (If not exist, restart RStudio)
The YAML header contains entries for general information, such as name, address, phone
number, and more. In the below, the header text introduces separate sections for topics like
personal information.
R Markdown resources
R Markdown documentation
RStudio's R Markdown documentation includes a series of tutorials that will help you learn
about the main features of R Markdown, including code chunks, output formats, notebooks,
interactive documents, and more. The tutorials include online lessons that you can complete
directly in your RStudio Cloud workspace.
R Markdown reference materials
RStudio has developed a reference guide and a cheat sheet that you can bookmark and use
whenever you practice writing R Markdown files.
• The R Markdown Reference Guide contains three sections: Markdown syntax, knitr
chunk options, and Pandoc options. The guide is super detailed and includes tons of
examples and explanations so that you can easily find the exact information you need
to customize your R Markdown documents.
• The R Markdown Cheat Sheet is a convenient summary of the different steps and
workflow processes for R. It also includes sections with abbreviated explanations of
knitr and pandoc chunk options, and other useful information to review or look up
while you work.
R for Data Science book
For a well-organized introduction to the basics of R Markdown, check out
the Communicate section of the R for Data Science book. It covers the main features and
functions of R Markdown, the various output formats, and the workflow for combining text
and code to create an analysis notebook.
**R Markdown: The Definitive Guide **
If you want to really explore the capabilities of R Markdown in a systematic way, R
Markdown: The Definitive Guide provides a comprehensive guide to the R Markdown
ecosystem. This book contains four main parts:
1. Part I explains how to install the relevant packages and offers an overview of R
Markdown, including the syntax for Markdown and code chunks.
2. Part II provides detailed documentation of the built-in output formats included in R
Markdown, like document formats and presentation formats.
3. Part III shares several R Markdown extension packages that allow you to build
different applications or generate output documents with different styles.
4. Part IV covers advanced topics in R Markdown.
Jupyter notebooks
Jupyter notebooks are documents that contain computer code and rich text elements –
such as comments, links, or descriptions of your analysis and results. You will find them used
in a variety of online tools, including Project Jupyter, Kaggle, and Google Colaboratory
("Colab" for short). These notebooks can be executable documents that you can run to
perform an analysis.
Jupyter notebooks can come in handy with everything from data cleaning and
transformation, to statistical modeling and visualizations. They are compatible with R, so you
can consider them as an alternative to R Markdown. And just like R Markdown documents,
you can easily share Jupyter notebooks with team members and stakeholders.
Appendix
R Markdown
R markdown allows you to put code and writing in the same place.
When you have written, executed, and documented your code in an R markdown document
like this, you can use the knit button in the menu bar at the top of the editing pane to export
your work to a beautiful, readable document for others.
R libraries - palmer penguins
[Link]('palmerpenguins')
library('palmerpenguins')
ggplot2
[Link]('ggplot2')
library('ggplot2')
R-versus-Python
Languages R Python
• R versus Python, a comprehensive guide for data professionals: This article is written
by a data professional with extensive experience using both languages and provides a
detailed comparison.
• R versus Python, an objective comparison: This article provides a comparison of the
languages using examples of code use.
• R versus Python: What’s the best language for data science?: This blog article
provides RStudio’s perspective on the R vs. Python debate.
• RStudio: A Single Home for R & Python
• Ways to learn about programming
Programming languages by profession
Data analyst
A data analyst collects, transforms, and organizes data to draw conclusions, make
predictions, and drive informed decision-making. The most popular programming languages
used by data analysts are R and Python.
R offers convenient statistical features for data analysis and is useful for creating advanced
data visualizations. Check out these resources to learn more about R:
• The R Project for Statistical Computing: a website for downloading R, documentation,
and help
• R Manuals: links to manuals from the R core team, including introduction,
administration, and help
• Coding Club R Tutorials: a collection of coding tutorials for R
• R for Beginners: a starting guide to help you work with data, graphics, and statistics in
R
Python is a general-purpose language that you can use to create what you need for data
analysis. Here are a few resources to begin learning Python:
• The Python Software Foundation (PSF): a website with guides to help you get started
as a beginner
• Python Tutorial: a Python 3 tutorial from the PSF site
• Coding Club Python Tutorials: a collection of coding tutorials for Python
Web designer
A web designer is responsible for the styling and layout of web pages containing text,
graphics, and video. Web designers generally use Hypertext Markup Language v5 (HTML5)
and Cascading Style Sheets (CSS) to create web pages.
HTML5 provides structure for web pages and is used to connect to hosting platforms. Learn
more about HTML5 and CSS using these resources:
• HTML Tutorial: an introduction to HTML with links to HTML5 features, examples, and
references
• HTML5 Cheat Sheet: a handy summary of HTML5 tags, attributes, and compatibility
with HTML4
• HTML5 and CSS Fundamentals course: a free W3C course on edX; a verified course
certificate can be issued for $199
CSS is used for web page design and controls graphic elements (color, layout, and font) and
page presentation on multiple devices (large screens, mobile screens, and printers). Check
out these cheat sheets for CSS:
• Interactive CSS Cheat Sheet: includes the most common CSS snippets for gradient,
background, font-family, border, and much more
• 50 Best HTML & CSS Cheat Sheets: a list of 50 cheat sheets–choose a few that are
useful to you
Mobile application developer
A mobile application developer uses programming to create applications used on laptops,
mobile phones, and tablets. The most popular programming languages for mobile
application developers are Swift, Java, and C#.
Swift (for Apple platforms) is an open source scripting language for macOS, iOS, watchOS,
and tvOS. Its main goal is to make applications run faster. Browse these resources for more
information about Swift:
• [Link]: an open source community with resources to learn how to use Swift,
including videos and sample code
• Swift developer site: an Apple developer website with information for developers
who want to use Swift
• Swift development resources: Apple’s collection of documentation, sample code,
videos, and recommended books
Java (for Android devices) is the official language for Android development. The article I
want to develop Android apps - which languages should I learn? explores some other
languages used for Android development. Check out these resources for Java:
• Android Studio: a downloadable integrated development environment (IDE) with
tools to build apps for Android devices
• Build your first Android app in Java: instructions for installing Android Studio and
creating your first app
• Java tutorial for beginners: write a simple app with no previous experience: an
overview of how to learn Java, with examples
C# (pronounced C-sharp) is an object-oriented programming language that is widely used to
create mobile apps in the .NET open source developer platform. Xamarin extends the .NET
platform with a framework for developers to create cross-platform mobile apps for both iOS
and Android. Here are a few resources to help you learn C#:
• Microsoft .NET learning materials for C#: includes free courses, tutorials, and videos
to learn the programming language C#
• Microsoft Xamarin learning materials: includes free courses, tutorials, and videos to
learn about mobile development with Xamarin
• Xamarin Tutorial - build your first iOS or Android app in C#: instructions for building a
mobile app that displays the text “Hello World”
• Learn C# from Codecademy: a website with free basic interactive lessons, and
additional activities that can be accessed with a monthly subscription
Web application developer
A web application developer designs and develops network applications used across the
web. The most popular programming languages used by web application developers are
Java, Python, Ruby, and PHP.
Java is widely used to create enterprise web applications that can run on multiple clients.
Java’s main strength is its “Write Once, Run Anywhere” (WORA) [Link] these
resources to learn more about Java:
• Oracle Java Tutorials: Java tutorials from Oracle documentation
• Java for Beginners: a free Java course for beginners from the website “Home and
Learn”
Python is a general-purpose programming language. Check out the Python resources listed
in the data analyst section.
Ruby is a general-purpose, object-oriented programming language used for web application
development. Ruby isn't the same as Ruby on Rails, which is an open source web application
framework that runs using Ruby. Browse these resources to learn more about Ruby:
• Ruby news: information about the latest Ruby releases and links to other resources
• Ruby documentation: includes guides, tutorials, and reference material to help you
learn more about Ruby
• Ruby programmer’s guide: a tutorial and reference guide for Ruby
• Learn Ruby from Codecademy: a website with free basic interactive lessons, and
additional activities that can be accessed with a monthly subscription
PHP is a scripting language particularly suited for web application development. It was based
on Perl, another programming language. PHP is simple, flexible, and relatively easy to learn.
Check out these resources to learn more about PHP:
• PHP downloads and documentation: information about the latest PHP releases and
links to other resources
• [PHP the Right Way]([Link] "This link takes you to the "PHP
The Right Way" page."): a quick reference for popular PHP coding standards
• Interactive PHP tutorial: a free tutorial that runs PHP code in exercises
Game developer
A game developer is an application developer who specializes in video game creation. Game
developers most commonly use the programming languages C# and C++.
C# is an object-oriented programming language that is widely used to create games. Check
out the C# resources listed in the mobile application developer section.
C++ is an extension of the C programming language that is also used to create console
games, like those for Xbox. Browse more information about C++:
• Microsoft resources for C++: learn how to install the Visual Studio IDE and write C++
code
• Microsoft C++ and C# code samples for gaming: a resource with over 40 C++ and C#
code samples for gaming
• Interactive C++ tutorial: a free tutorial that runs C++ code in exercises
Tips for learning programming languages
Here are a few tips to follow when you start learning a new programming language:
• Define a practice project and use the language to help you complete it. This makes
the learning process more practical and engaging.
• Keep previous concepts and coding principles in mind. Many of these are
transferable between programming languages. So, after you have learned one
language, learning a second or third programming language tends to be much easier.
• Create and keep good notes and cheat sheets in whatever format (handwritten or
typed) that works best for you.
• Create an online filing system for information that you can easily access while you
work in various programming environments.