Try   HackMD

Workshop Details
Dates: September 6th - 13th, 2022
Time: 9am - 12pm

Workshop Agenda:
https://ucsdlib.github.io/2022-09-06-carpentries-uc/

Lesson:
https://swcarpentry.github.io/r-novice-gapminder/

Day 1 - 3: Introduction to R

Software Installation:

Lesson Data (download)

NOTES:

A copy of the instructor live session notes will be made available to participants upon request at the end of the workshop.

Workshop Day 1 (83)

First name and Last Name/Organization/Dept./Email

Name (first & last) Organization Dept. Email
Joanna Eckhardt UCSD Neurosciences jeckhardt@ucsd.edu
Kyle Rokes UCSB -kyle_rokes@ucsb.edu
(example) Jane Doe UCSD IT jdoe1@ucsd.edu
Juan Sanchez Arcila (helper) UCM
Chelsea Chapman UCSD Public Health crchapma@health.ucsd.edu
Ha Vu UCSD Economics vha@ucsd.edu
Yulissa Perez Rojas UCM ES yperezrojas@ucmerced.edu
Shang Su The University of Toledo Cell and Cancer Biology shang.su@utoledo.edu
Guru Kalyan Jayasingh UCSD Physics gjayasingh@ucsd.edu
Brittany Johnson UCM Cognitive Science bjohnson44@ucmerced.edu
Daira Melendez UCSD Bioinfo damelendez@ucsd.edu
Tom Le UCM tle267@ucmerced.edu
Govind Sah UCSD Pathology gsah@health.ucsd.edu
Brett Taylor UCSD Biomedical Sciences b5taylor@ucsd.edu
Osika Tripathi UCSD Public Health otripathi@health.ucsd.edu
Nicole UCLA Master of Urban Planning nlmatteson@g.ucla.edu
Kenan Chan UCSD SCRIPPS kmc001@ucsd.edu
Felipe Vásquez UCSD Bioinformatics fvasquezcastro@ucsd.edu
Alexander Bernal UCSD Economics a1bernal@ucsd.edu
Roberto Silva UCSD Scripps rosilva@ucsd.edu
Melodi Frey UCSD CMM mtastemel@health.ucsd.edu
Dexin Zhou UCSD Mathematics dzhou@ucsd.edu
Dane Whicker UCSD SIO dwhicker@ucsd.edu
Melisa Rodriguez UCSD Scripps mdr013@ucsd.edu
Alexander Frey UCSD Rady School of Management alexander.frey@rady.ucsd.edu
Cemil Can Ali Marandi UCSD BISB calimarandi@ucsd.edu
Fangke Jiang UCSD economics fjiang@ucsd.edu
Oishee Misra UCSD Economics omisra@ucsd.edu
Jessica Murillo UCSD Med jdmurillosaich@health.ucsd.edu
Steven Krehel UCSD Economics skrehel@ucsd.edu
Melodi Frey UCSD CMM mtastemel@health.ucsd.edu
Govind Sah UCSD Pathology gsah@ealth.ucsd.edu
m Ghebremedhin UCSD aghebremedhin@health.ucsd.edu
Alex Chaim UCSD CMM iachaim@ucsd.edu
Alissa Jae Lazo Kim UCSD DBMI internship alazokim@health.ucsd
Mugen Blue UCM EECS mblue3@ucmerced.edu
Glen MacDonald
Laura Lopez UCM lgarcialopez2@ucmerced.edu
Danny Lee UCSD Economics dannytylee@ucsd.edu
Edgar Reyna UCLA MURP eareyna@ucla.edu
Sarina Qin UCM sqin@ucmerced.edu
AJ Rice UCSB Political Science riceaj@ucsb.edu
Julia Alvarez UCM Nat Sci jhampton3@ucmerced.edu
Nicole Rosenberg UCSD Scripps nrosenberg@ucsd.edu

Day 1 Questions:

Please enter any questions not answered during live session here:
1.

End Day 1


Workshop Day 2

First name and Last Name/Organization/Dept./Email

Name (first & last) Organization Dept. Email
Kyle Rokes UCSB kyle_rokes@ucsb.edu
Agnieszka Pluta UCLA agpluta@ucla.edu
Alexander Bernal UCSD Economics a1bernal@ucsd.edu
Eastern Kang (helper) UCSD Pediatrics dkangsim@health.ucsd.edu
Jessica Murillo UCSD Med jdmurillosaich@health.ucsd.edu
Reid Otsuji (Instructor) UCSD Library rotsuji@ucsd.edu
Dane Whicker UCSD SIO dwhicker@ucsd.edu
Fangke UCSD Economics fjiang@ucsd.edu
Cemil Can Ali Marandi UCSD BISB calimarandi@ucsd.edu
Oishee Misra UCSD Economics omisra@ucsd.edu
Brittany Johnson UCM Cognitive Science bjohnson44@ucmerced.edu
Steven Krehel UCSD Economics skrehel@ucsd.edu
Roberto Silva UCSD Scripps rosilva@ucsd.edu
kenan Chan UCSD SIO
Alexander Frey UCSD Rady School of Management alexander.frey@rady.ucsd.edu
Melisa Rodriguez UCSD Scripps mdr013@ucsd.edu
Anghesom Ghebremedhin UCSD aghebremedhin@health.ucsd.edu
Ha Vu UCSD Economics vha@ucsd.edu
Melodi Frey UCSD CMM mtastemel@health.ucsd.edu
Shang Su U Toledo Cell and Cancer Biology shang.su@utoledo.edu
Alex Chaim UCSD CMM iachaim@ucsd.edu
Osika Tripathi UCSD Public Health otripathi@health.ucsd.edu
Chelsea Chapman UCSD Public Health crchapma@health.ucsd.edu
Glen MacDonald UCLA Geog glen@geog.ucla.edu
Alissa Jae Lazo Kim UCSD DBMI internship alazokim@health.ucsd.edu
Dexin Zhou UCSD Mathematics dzhou@ucsd.edu
Brett Taylor UCSD Biomedical Sciences b5taylor@ucsd.edu
Juan Sanchez Arcila (helper/carpentries instructor) UCM jsanchezarcila@ucmerced.edu
Susana Tejeda-Garibay UCM Quantitative Systems Biology stejeda-garibay@ucmerced.edu
Laura Lopez University of California Merced lgarcialopez2@ucmerced.edu
Joanna Eckhardt UCSD Neurosciences jeckhardt@ucsd.edu
Cristian Gruppi UCLA IoES cristiangruppi@g.ucla.edu
Mugen Blue UCM EECS mblue3@ucmerced.edu
Julia Alvarez UCM Nat Sci jhampton3@ucmerced.edu
Danny Lee UCSD Economics dannytylee@ucsd.edu
Felipe Vasquez UCSD Bioinformatics fvasquezcastro@ucsd.edu
Edgar Reyna UCLA MURP eareyna@ucla.edu
Daira Melendez UCSD Bioinformatics damelendez@ucsd.edu
Yulissa Perez Rojas UCM Environ. yperezrojas@ucmerced.edu
Sarina Qin UCM sqin@ucmerced.edu
AJ Rice UCSB Political Science riceaj@ucsb.edu

Day 2 Shared Notes

Subsetting Data

We can subset using corresponding indices. To demonstrate this, let's create a simple numeric vector:

x<- c(5.4, 6.2, 7.1, 4.8, 7.5)

#any number would do. You can also assign names to the numeric vectors.

names(x) <- c('a', 'b', 'c', 'd', 'e')

remember c() denots for combine or concatenate.

To extract a single value, we can use the square bracket []. For example, if we use x[1], where x is the object of we created and [1] is the corresponding index.

We can use the c() and extract multiple elements at once.
x[c(1,3)] would extract first and third elements from the vector.


As for the setwd(), it is best practice to have the user running the script begin in a consistent directory on their machine and then use relative file paths from that directory to access files.


We will be using the Dplyr and Tidyr packages that are a part of the Tidyverse package.

If. you have not instaleld the tidyverse package, you can install it using the following command:

install.packages('tidyverse')

NOTE: installing a package needs to be done only once

library(tidyverse)

NOTE: loading library needs to be done every time you run a new R session.

(for more information about the tidyverse, you can visit: https://www.tidyverse.org)

Previously we used x for our object name; let's use the same approach and load the .csv dataset and call the object gapminder.

gapminder <- read.csv("working_directory/gapminder_data.csv")

Alternative way of loading the dataset:

gapminder <- read.csv(file.choose())

a new window will pop-up and you can select the file you want to load.


Using the dplyr package, we can subset variable names.

year_country_gap <- 
    select (gapminder, year, country, gdpPercap)

If you have multiple pacakges loaded, some function share the same name. To avoid any conflict, you may specify the package name.

year_country_gap <-
    dplyr::select(gapminder, year, country, gdpPercap)

The :: tells R console that we want to use a function from a specific package.


The pipe operator %>% makes the data wrangling process much easier and smoother. The pipe basically means passing down (streaming down) information from one script to the next. Having multiple functions in one command improves the thinking process and improves readability of the script.

year_count <- gapminder %>% 
    select (year, country, gdpPercap)

We can rename an existing variable name:

tidy_gdp<- year_country_gdp %>% 
    rename(gdp_per_capita = gdpPercap)

You can also choose to do all these in one chunk of codes using the %>%

tidy_gdp<- gapminder %>% 
    select(year, country, gdpPercap) %>%
    rename(gdp_per_capita = gdpPercap)

Now we are going to cover the filter() function. Previously we covered the select() function, which subsets data by the column (variable) names. The filter() function subsets data by rows.

year_country_gdp_euro <- gapminder %>%
    filter (continent == "Europe") %>%
    select(year, country, gdpPercap)

Basically, the script above means: take the gapminder dataset, subset it by Europe, and select variables year, country, and gdpPercap.

By using the pipe operator, %>%, we can tell the story of how our data were manipulated.

Another example:

europe_lifeExp_2007 <- gapminder %>% 
    filter (continent =="Europe", year ==2007)%>% 
    select(country, lifeExp)

Challenge 1

Write a single command (which can span multiple lines and includes pipes) that will produce a data frame that has the African [or any other country you pick] values for lifeExp, country and year, but not for other Continents. How many rows does your data frame have and why?

lifeExp_africa_asia<- gapminder%>% 
   filter(continent == c("Africa", "Asia"))%>%
   select(year, country, lifeExp)

Instead of using the filter() function, which will only pass observations that meet your criteria, we can use the group_by(), which will use every unique criteria that you could have used in filter.

Example:

gdp_bycontinents <- gapminder%>%
   group_by(continent) %>%
   summarize(mean_gdp_Percap = mean(gdpPercap))

The script above basically states: take the gapminder dataset, group values by the variable continent, then summarize values (mean, sd, etc.). The end result should be the mean gdpPercap for each continent.


Challenge 2

Calculate the average life expectancy per country. Which has the longest average life expectancy and which has the shortest average life expectancy?

lifeExp_bycountry <- gapminder %>%
   group_by(country) %>%
   summarize(mean_lifeExp = mean(lifeExp))

lifeExp_bycountry %>%
   filter(mean_lifeExp == min(mean_lifeExp) | mean_lifeExp == max(mean_lifeExp))

Depends on the research questions, we may use the function group_by() to group multiple variables.

gdp_bycontinents_byyear <- gapminder %>%
    group_by(continent, year) %>%
    summarize(
        mean_gdpPercap = mean(gdpPercap),
        sd_gdpPercap = sd(gdpPercap))

Sometimes, instead of summarizing informatoin, we want to create a new variable. The mutate() function creates a new variable (adds a new column into the dataset).

gap_GDP <- gapminder%>% 
    mutate(gdp = gdpPercap * pop)

The script above states: take the gapminder data, create a new variable called gdp which is defined as multiplication between gdpPercap and pop variables.

We can extend the script a bit further:

gdp_pop_bycontinents_byyear <- gapminder %>%
    mutate(gdp_billion = gdpPercap*pop/10^9) %>%
    group_by(continent,year) %>%
    summarize(mean_gdpPercap = mean(gdpPercap),
              sd_gdpPercap = sd(gdpPercap),
              mean_pop = mean(pop),
              sd_pop = sd(pop),
              mean_gdp_billion = mean(gdp_billion),
              sd_gdp_billion = sd(gdp_billion))

By following the %>% operator, we can follow the thinking process of our data manipulation. This improves readability and make the script more reproducible.

Day 2 Questions:

Please enter any questions not answered during live session here:
1.

End Day 2

Workshop Day 3

First name and Last Name/Organization/Dept./Email

Name (first & last) Organization Dept. Email
Joanna Eckhardt UCSD Neurosciences jeckhardt@ucsd.edu
Alexander Frey UCSD Rady School of Management alexander.frey@rady.ucsd.edu
Melodi Frey UCSD CMM mtastemel@health.ucsd.edu
Cemil Can Ali Marandi UCSD BISB calimarandi@ucsd.edu
Chelsea Chapman UCSD Public Health crchapma@health.ucsd.edu
Yulissa Perez Rojas UCM Environ. yperezrojas@ucmerced.edu
Kenan Chan UCSD UCSD SIO
Alissa Jae Lazo-Kim UCSD UCSD DBMI internship
Brittany Johnson UCM Cognitive Science bjohnson44@ucmerced.edu
Anghesom Ghebremedhin UCSD aghebremedhin@health.ucsd.edu
AJ Rice UCSB Political Science riceaj@ucsb.edu
Melisa Rodriguez UCSD Scripps mdr013@ucsd.edu
Osika Tripathi UCSD Public health otripathi@health.ucsd
Dane Whicker UCSD SIO dwhicker@ucsd.edu
Jessica Murillo UCSD Medicine jdmurillosaich@health.ucsd.edu
Kat Koziar UCR Library katherine.koziar@ucr.edu
Danny Lee UCSD Economics dannytylee@ucsd.edu
Oishee Misra UCSD UCSD Economics
Ha Vu UCSD Economics vha@ucsd.edu
Shang Su U Toledo Cell and Cancer Biology shang.su@utoledo.edu
Marta Sala Climent UCSD Medicine msalacliment@health.ucsd.edu
Roberto Silva UCSD Scripps rosilva@ucsd.edu
Mugen Blue UCM EECS mblue3@ucmerced
Alexander Bernal UCSD Economics a1bernal@ucsd.edu
Edgar Reyna UCLA Urban planning eareyna@ucla.edu
Felipe Vasquez Castro UCSD Bioinformatics fvasquezcastro@ucsd.edu
Dexin Zhou UCSD Mathematics dzhou@ucsd.edu
Daira Melendez UCSD Bionformatics damelendez@ucsd.edu
Julia Alvarez UCM Nat sci jhampton3@ucmerced.edu
Steven Krehel UCSD Economics skrehel@ucsd.edu
Govind Sah UCSD Pathology gsah@health.ucsd.edu
Sarina Qin UCM Nat Sci sqin@ucmerced.edu
Brett Taylor UCSD Biomedical Sciences b5taylor@ucsd.edu

Day 3 Shared Notes

For when your computer doesn't have admin permissions to install things like packages, RStudio Cloud is a good option

Today we'll be plotting using the ggplot2 package, which is a popular R plotting package

Today's notes and cheat sheet: https://hackmd.io/3M1KawkLRhezD0u1TAkpfQ?view

ggplot2 cheat sheet

Once ggplot is installed, we can call functions from this package by calling the

library(ggplot2)
ggplot(data = gapminder_csv, mapping = aes(x = gdpPercap, y = lifeExp)) + geom_point()

Challenge 1

Part A
In the gapminder example we’ve been using,

ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) + 
  geom_point()

use the column “year” to show how life expectancy has changed over time.

Solution:

ggplot(data = gapminder, mapping = aes(x = year, y = lifeExp)) + geom_point()

But this doesn't look great, does it? So, Part B:

Part B
We’ve been using the aes function to tell the scatterplot geom about the x and y locations of each point. Another aesthetic property we can modify is the point color. Modify the code from the Part A to color the points by the “continent” column. Is it easier to detect trends?

ggplot(data = gapminder, mapping = aes(x = year, y = lifeExp, color=continent)) +
  geom_point()

We can use color argument in aes() to specify we want points colored by value of continent

Geom arguments

If we want to use lines rather than a scatterplot, we can use geom_line()

  geom_line()

Instead of adding a geom_point layer, we’ve added a geom_line layer.

However, the result doesn’t look quite as we might have expected: it seems to be jumping around a lot in each continent. Let’s try to separate the data by country, plotting one line for each country:

ggplot(data = gapminder, mapping = aes(x=year, y=lifeExp, by=country, color=continent)) + 
geom_line()

Note the by = additional portion

We’ve added the by aesthetic, which tells ggplot to draw a line for each country.

But what if we want to visualize both lines and points on the plot? We can add another layer to the plot:

ggplot(data = gapminder, mapping = aes(x=year, y=lifeExp, by=country, color=continent)) +
  geom_line() + geom_point()

It’s important to note that each layer is drawn on top of the previous layer. In this example, the points have been drawn on top of the lines. Here’s a demonstration:

ggplot(data = gapminder, mapping = aes(x=year, y=lifeExp, by=country)) +
  geom_line(mapping = aes(color=continent)) + geom_point()

The items within ggplot() aes() apply to the entire plot, but can override with aes() command in individual geom()

Challenge 2

Switch the order of the point and line layers from the previous example. What happened?

ggplot(data = gapminder, mapping = aes(x=year, y=lifeExp, by=country)) +
 geom_point() + geom_line(mapping = aes(color=continent))

Now the lines are on top of the points

Transformations and trend lines

We can adjust plots in other ways, including transformations and overlaying statistical models

Returning to a scatterplot:

ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

To change the x axis to a log scale using scale_x_log10():

ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
  geom_point() + scale_x_log10()

And to change the transparency of the points, since there's lots of overplotting, we can use alpha in geom_point():

ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
  geom_point(alpha = 0.5) + scale_x_log10()

To add a trend line, we can use geom_smooth() and specify the type of model (here, a linear model, with method = 'lm'):

ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
  geom_point(alpha = 0.5) + scale_x_log10() + geom_smooth(method="lm")

And can change line thickness using size in geom_smooth():

ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
  geom_point(alpha = 0.5) + scale_x_log10() + geom_smooth(method="lm", size=1.5)

Challenge 3

Part A
Modify the color and size of the points on the point layer, but don’t use the aes() function in that layer.

ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
 geom_point(size=3, color="orange") + 
 scale_x_log10() +
 geom_smooth(method="lm", size=1.5)

Note the color = "orange" and size = 3 within geom_point()

Part B
Modify your solution to Part A so that the points are now a different shape and are colored by continent with new trendlines. Hint: The color argument can be used inside the aes() function.

ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, color = continent)) +
 geom_point(size=3, shape=17) + 
 scale_x_log10() +
 geom_smooth(method="lm", size=1.5)

want trend line for each continent:

ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, color = continent)) +
 geom_point(size=3, shape=17) + 
 scale_x_log10() +
 geom_smooth(method="lm", size=1.5) 

Multi-panel plots

Subset to only Americas data

americas <- gapminder[gapminder$continent == "Americas",]

Then use ggplot() with facet_wrap() which lets us facet by variable (in this case, by country variable):

ggplot(data = americas, mapping = aes(x = year, y = lifeExp)) +
  geom_line() +
  facet_wrap( ~ country) +
  theme(axis.text.x = element_text(angle = 45))

This gives us a panel for each country

The theme() command lets us specify things like x and y axis text and angles. In this case, we're specifying axis.text.x to say we want the x axis text to be at a 45 degree angle.

To modify text:

To clean this figure up for a publication we need to change some of the text elements. The x-axis is too cluttered, and the y axis should read “Life expectancy”, rather than the column name in the data frame.

We can do this by adding a couple of different layers. The theme layer controls the axis text, and overall text size. Labels for the axes, plot title and any legend can be set using the labs function. Legend titles are set using the same names we used in the aes specification. Thus below the color legend title is set using color = "Continent", while the title of a fill legend would be set using fill = "MyTitle".

ggplot(data = americas, mapping = aes(x = year, y = lifeExp, color=continent)) +
  geom_line() + facet_wrap( ~ country) +
  labs(
    x = "Year",              # x axis title
    y = "Life expectancy",   # y axis title
    title = "Figure 1",      # main title of figure
    color = "Continent"      # title of legend
  ) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

Exporting the plot:

We want to assign our plot to an object (here naming it lifeExp_plot, then use ggsave to export the plot to png (specifying file format in output name, here "lifeExp.png")

lifeExp_plot <- ggplot(data = americas, mapping = aes(x = year, y = lifeExp, color=continent)) +
  geom_line() + facet_wrap( ~ country) +
  labs(
    x = "Year",              # x axis title
    y = "Life expectancy",   # y axis title
    title = "Figure 1",      # main title of figure
    color = "Continent"      # title of legend
  ) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

ggsave(filename = "results/lifeExp.png", plot = lifeExp_plot, width = 12, height = 10, dpi = 300, units = "cm")

Here we're specifying we want a png as output, width of 12 cm, height of 10 cm, with dpi (basically, resolution) of 300

Producing reports with knitr

Check the notes for screenshots: https://hackmd.io/3M1KawkLRhezD0u1TAkpfQ?view#Create-Reports-with-knitr

(Fun fact, this document uses markdown! You can see how we're making items bold, or putting in code chunks, by selecting the icon in the top banner between the pencil and the eye, to see the raw markdown on theput on the right.)

Challenge

Question 1
Create a new R Markdown document. Delete all of the R code chunks and write a bit of Markdown (some sections, some italicized text, and an itemized list).

Convert the document to a webpage.

Question 2
Load the ggplot2 package
Read the gapminder data
Create a plot

Day 3 Questions:

Please enter any questions not answered during live session here:

  1. How to pick a color for a specific subset of data
    2. (see above - we did cover this)

Other R resources

R graph gallery - https://r-graph-gallery.com/ (specific to ggplot 2 - https://r-graph-gallery.com/ggplot2-package.html)
R markdown cookbook - https://bookdown.org/yihui/rmarkdown-cookbook/
R for Data Science (data viz chapter) - https://r4ds.had.co.nz/data-visualisation.html

End Day 3