Reid Otsuji
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: gps --- # 2021-GPS-Data-Skills-Course-intro-r-notes ## Collaborative Notes: first name last name Belinda Panelo Wooseok Choi Yongun Ra Bonnie Devenney Shelby Sellers Nikki Jazayeri Daniela Hall-Lagunes Amanda Lee-Low Ama Debrah Ada Tong Halle Sousa Harrison Tang Hannah Kase Woods Tyler Spencer Chase Farrell Swati Nair Khang Do Alejandra Guzman Ayush Jain Camila Urzúa Isaac Wang Chandler Katschke Sam Yeung Cate Pollock Manabu Hiratsuka William Gao Lei Lei Anthony Nguyen Bowin Lee Alexander Schiller Elissa Bozhkov Xin Meng Rachel Lietzow Claudia Fernández Daniel Blaugher Emily Carlton Leo Do Jennifer Escobar Mario Heredia Dizhi Wang Morgan Furr Lu Pang Deepika Bagaria Adrian R. Valdez Eeshita Chib (Esha) Ziqra Raza Kenneth Cashin Brown Jack Alegre Colin Trobough Jin Huang Zahrah Zimmerer Michael Hatch? Jonathan Bazan Laura Nora Audrey Lau Anran Shi Steven Zhang Ben Swearingen ###########Notes here######### # 01/05/2020, Lecture 1, Intro to R and RStudio ## Basic Navigation Writing your code in r file (file -> new file -> R Script) Use # to make notes (not executed as code) ## Basic RStudio layout: When you first open RStudio, you will be greeted by three panels: The interactive R console/Terminal (entire left) Environment/History/Connections (tabbed in upper right) Files/Plots/Packages/Help/Viewer (tabbed in lower right) * Key command to run code: ``` crtl + enter cmd + enter ``` * assignment operator: ``` <- * Quick trick, the hotkey for <- is "alt"+"-" ``` R can used for basic math #hash tag is used for commenting ``` # Create a variable called "abc" with value 34 # Create a variable called "xyz" with value "abc" + 78 # What is the value of "xyz" abc <- 34 xyz <- abc + 78 ``` ## Logical operator ``` < # less than > # greater than == # equal to <= # less or equal >= # greater or equal ``` Difference between "=" and "==" * "=" is for variable assignment, and in R, it is preferred to use "<-" for assignment. > Example: x = 5 is equivalent to x <- 5, which means create a number 5, and assign this value to your variable x * "==" is a binary operator (i.e. a sign that requires two inputs) that is used to compare the equivalence of two objects. > Example: 5 == 5 will return TRUE > 5 == 4 will return FALSE Question about parentahsis: "()" is used when you call a function (i.e. a piece of program you or someone else wrote before) ls() #shows list of variables Variable name formatting is important * do not put spaces in variable names or use `.` * be consistent when creating variable names e.g. * no spaces * underscores * camelcase ### looking for R packages use: cran-project.org ### installing packages use the packages tab in the bottom right RStudio pane. * install icon * or checkmark package you want to load Rtools https://cran.r-project.org/bin/windows/Rtools/ ``` #loading a library in code: library(ggplot2) ``` ### saving file: save as ##################Lesson 2######################## # 01/07/2020, Lecture 2 ## Vector ``` 1:5 # same as a collection of 1,2,3,4,5 ``` **Broadcasting**: simultaneously operate on every number of the vector ``` x <- c(1,2,3,4,5) x ^ 2 # output 1,4,9,16,25 ``` **Dataframe** is a 2D tabular data ``` # creation of dataframe from vectors newdaataFrame <- data.frame(a, y, z) # view dataframe View(newDataFrame) # shows the infomration about the dataframe ``` ### Loaing gapmidner data data file gapminder.csv is available in canvas telling R where to find the gapminder dataset ``` Session/set working directory/choose directory/ the location where you saved the data ``` loading data in to R ``` gapminder <- read.csv(file = "gapminder.csv") ``` looking at the gapminder dataset details ``` View(gapminder) str(gapminder) #str = structure displays the rows and columns and the data types ``` gapminder data has: 1704 obs = observations / rows 6 variables = variables / columns Data types: Factors, integers, numeric, factor, ``` #Exploring data types using class(): t <- 2 class(t) u <- "2" #changing data types r <- as.integer(t) # converts data type to an integer as.numeric # convert variable into numeric type as.character # convert variable into character type ``` Check current working directory location ``` getwd() # displays the working directory you are working in for the R session ``` Usage of double quote: double quote is used to denote a variable of string type. ``` key # this is a variable "key" # this is a string ``` Exercise: 1. Create a vector c numbers with every number bewteen 1- and 20 2. what type is it? 3. create a vector called name with your first and last name 4. what type is it? Solution: ``` numbers <- 10:20 numbers2 <- c(10,11,12,13,14,15,16,17,18,19,20) name <- c("Rick","McCosh") ``` working with gapminder datafram and continent column gapminder$continent $ indicw atates a column in the dataframe e.g. dataframe$columnname **arguments** are the objects you put into your function when you invoke the function. > example: summary(x), x would be called an argument ``` unique(x) # Gives you unique values, strip away the repeated values in vector x length(x) # tell you the number of elements inside of x summary(gapminder$lifeExp #summar mean(gapinder$pop) #calculates mean sd(gapminder$pop) #standard deviation min(gapinder$pop) #minimum max(gapminder$pop) #maximum ``` ``` names(gapminder) #tells you the name of each column head(gapminder) #displays first 6 rows of the dataset head(gapminder, 10) # displays the first top 10 rows of the data frame tail(gapminder) tail(gapminder, 20) #displays the last bottom 20 rows ``` ### Using the package Knitr you will have to istall the Knitr ``` install.packages("knitr") #download and installs the Knitr package library(knitr) #loads the knitr package in R for use ``` create an Rmarkdown document goto: ``` File menu/new file/R Markdown... ``` # #####Week 2 R Data Wrangling######## ## Tidy data format Rows = observations columns = variables ## using dplyr package start new rscript file save to desktop ``` # use hash tag to make comments ``` set working directory from Session menu in RStudio ``` setwd() #R function to set the working directory location ``` loading a package ``` library(dplyr) #load package ``` Read in data: ``` gapminder <- read.csv("gapminder.csv") ``` display the structure of the data ``` str(gapminder) #displays the structure of the data file head(gapminder) #displays the first several rows in the data summary(gapminder) #displays summary of the data ``` ### working with data #### ``` continent_data <- select(gapminder, continent) #subset data into a new data frame year_country_gdp_data <- select(gapminder, year, country, gdpPercap) #create another subset of data head(year_country_gdp_data) # displays the first several lines ``` Filter rows based on condition ``` canada_data <- filter(gapminder, country == "canada") #filter only canada head(canada_data) unique(canada_data$country) #display only unique ``` ``` portugal_data <- filter(gapminder, country =="Portugal") #filter portugal ``` combining functions using `%>%` `control+shift+m` (windwos) `command+shift+m` (Mac) ``` #filtering dataset country_year_pop_recent_only <- gapminder %>% filter(year >= 1977) %>% select(country, year, pop) ``` ``` canugal_recent <- gapminder %>% filter(country == "Canada" | country == "Portugal" ) %>% filter(year >= 1992) ``` ``` canugal_recent_redo <- gapminder %>% filter(country == "Canada" | country == "Portugal" & year >= 1992) %>% select(country, year, pop) ``` when writing fitlers make sure to: run `filter` statement first then `select` tab for auto completion > example: when type read.csv("gapmin[tab]"), R autocomplete to read.csv("gapminder.csv") command (control) + enter for executing a line of code bri ``` gap_africa <- gapminder %>% filter(continent == "Africa" ) %>% select(lifeExp, country, year) str(gap_africa) ``` select() #is for columns filter() #is for rows $ #represents column e.g. gapminder$lifExp #### object not found errors or other errors check for typos first forgetting commas or closign parenthesis observations #dataframe rows variaables #dataframe columns how to make a new column ``` #left of = is name of new column #right of = is data in new column gap_extra <- gapminder %>% mutate(ratio = lifeExp/gdpPercap) ``` is there a command that selects everything but one specific column that you want to get rid of? ``` select(-lifeExp) #minus in select will remove columns ``` # #####Week 2 Day 4 R notes######## ## Recap Load dplyr ``` library(dplyr) ``` set working directory ``` setwd("~/Desktop") ``` or use the command under the `session` menu in RStudio read in the data: ``` gapminder <- read.csv("gapminder.csv") ``` #data set informatin - structure, head, summary ``` str(gapminder) head(gapminder) summary(gapminder) ``` Recap 3 dplyr fucntions select() #select columns filter() #select rows mutate() #create new column ``` gap_gdp <- gapminder %>% mutate(gdp = gdpPercap *pop) %>% # this will create a new column called gdp filter(continent == "Europe") %>% #select Europe as the continent data select(country, year, pop) #display the columns country, year, pop head(gap_gdp) #display first several rows from the new subset gap_gdp ``` creating a variable you use `<-` "assignment symbol" in dplyr mutate uses single `=` to create the column pipe `%>%` chains functions together. It "pipes" the output from the last function into the input of the next function Also, just to show how miserable life is without pipe, writing gap_gdp without pipe looks like this: ``` select(filter(mutate(gapminder, gdp=gdpPercap * pop) , continen=="Europe"), year, pop, gdp) ``` Does R change your underlying data automatically? > No. All modification is done on the object inside R. Unless you explicitly export your object, the underlying data would not change. Example 2: I want to find the gdp to life expenctancy ratio for all countries in Oceania since 1992 and I only want the columns of the ratio, country name, and year ``` #adding gdp column to the main gapminder data frame gapminder <- gapminder %>% mutate(gdp = gdpPercap * pop) ``` ``` gapminder_oceania <- gapminder %>% mutate(gdp_lifeexp_ration = gdp / lifeExp) %>% filter(continent == "Oceania" & year >= 1992) %>% select(country, year, gdp_lifeexp_ratio) gapminder_oceania ``` ``` #dataframe with a columnb indicating if life expectancy is at least 50 years for all countries in Asia & africa and I only want the columns with country, year, and if life expectancy is over 50 years gap_century <- gapminder %>% mutate(half_century = ifelse(lifeExp >= 50, "over 50", "under 50")) %>% #adding `ifelse` statement and new column `half_centruy` filter(continent == "Asia" | continent == "Africa") %>% select(country, year, half_centruy) head(gap_century, 20) ``` ##### missing values you might see ".", " ", NA, -999, 0 R handles missing data as NA ``` gap_missing <- gapminder %>% mutate(missing = ifelse(lifeExp < 30, NA, "not missing")) %>% filter(is.na(missing)) gap_missing <- gapminder %>% mutate(missing = ifelse(lifeExp < 30, NA, "not missing")) %>% filter(!is.na(missing)) #add `!` to !is.na() to specify missing values ``` ``` gap_oceania_missing <- gapminder %>% mutate(new_column = ifelse(continent == "Oceania" & year != 1992, NA, "this is a test")) gap_oceania_missing %>% filter (is.na(new_column)) ``` group_by() and summarize() dplyr functions ``` gap_max_life <- gapminder %>% group_by(continent) %>% sumarize(max_life = max(lifeExp)) gap_max_life ``` ``` gap_max_life_country <- gapminder %>% group_by(continent, country) %>% sumarize(max_life = max(lifeExp)) gap_max_life_country ``` Exercsie i want to find the mean gdp per continent in 2007 for Europe and Asia ``` gap_mean_gdp <- gapminder %>% filter(year == 2007 & continent == "Europe" | continent == "Asia") %>% group_by(continent) %>% summarize(mean_gdp = mean(gdp)) gap_mean_gdp ``` ``` gap_new <- gapminder %>% group_by(country) %>% mutate(max_lifeExp = max(lifeExp)) gap_new ``` ``` gap_new2 <- gapminder %>% group_by(country) %>% mutate(max_lifeExp = max(lifeExp)) filter(gdpPercap < 1000) gap_new2 ``` extra dplyr ``` #using %in% to simplify gapminder %>% filter(country == "Canada" | country == "Mexico" | country == "Spain") gpminder %>% filter(country %in% c("Canada", "Mexico", "Spain")) #using the %in% ``` arrange() - orders the rows of a data frame by the values of selected columns. writing out data to to csv write.csv(gap_new, "gap_new.csv", row.names = FALSE) visualization ![](https://i.imgur.com/T6Ivc6v.png) # ###Week 3 R - lesson 5####### #### ggplot2 CHEATSHEET https://rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf we will be using the GGPLOT2 package set working directory to Desktop ``` setwd("~/Desktop") ``` or you can set working directory under the `sessions` menu. ``` # load libraries library(ggplot2) library(dplyr) ``` #making your first plot ``` # load gapminder dataset gapminder <- read.csv("gapminder.csv") # first ggplot function ggplot(data = gapminder, aes(x= gdpPercap, y=lifeExp)) + geom_point() # specify data # specify asthetic axis aes(x, y) # choose geom_[style]() ``` aes() = asthetic ##commenting tip: selelct multiple lines crtl+shift+c will set comment # for multiple lines Saving plots 1. exprot button in interface 2. ggsave() #function plots will save in the working directory 3. pdf(), or png(), jpeg() #save as pdf, png, or jpeg functions ``` # save to PDF example: pdf("lifeExp_time.pdf", width=8, height=8) ggplot(data = gapminder, aes(x=gdpPercap, y=lifeExp)) + geom_point() dev.off() #stops outputting code to PDF ``` ``` ggplot(data = gapminder, aes(x= gdpPercap, y=lifeExp)) + geom_point()+theme_classic() #added theme function ``` ``` #line graph ggplot(data = gapminder, aes(x=year, y=lifeExp, by = country)) + geom_line() #by = country tells ggplot to show a line for each country ``` ``` #assign plot to a variable `p` (lowercase p used) p <- ggplot(data = gapminder, aes(x=year, y=lifeExp, by = country)) #adding geom and theme functions to the plot: p <- p + geom_line() + theme_classic() p #displays plot ``` ``` #assign plot to a variable `p` p <- ggplot(data = gapminder, aes(x=year, y=lifeExp, by = country)) #adding geom and theme functions to the plot: p <- p + geom_line() + geom_point() = theme_classic() #added geom_point() to code p #display plot ``` ``` #adding colors #assign plot to a variable `p` p <- ggplot(data = gapminder, aes(x=year, y=lifeExp, by = country, color = continent)) # added `color` here #adding geom and theme functions to the plot: p <- p + geom_line() + theme_classic() p ``` ``` #assign plot to a variable `p` p <- ggplot(data = gapminder, aes(x=year, y=lifeExp, by = country, color = continent)) # added `color` here #adding geom and theme functions to the plot: p <- p + geom_line() + geom_point(color = "black") + theme_classic() #changed point color to black p ``` ``` #assign plot to a variable `p` p <- ggplot(data = gapminder, aes(x=year, y=lifeExp, by = country, color = continent)) p <- p geom_point(color = "black") + geom_line() + theme_classic() #changed order of points and lines p ``` ``` #changing geom_line() color p <- ggplot(data = gapminder, aes(x=year, y=lifeExp, by = country, color = continent)) p <- p geom_point(color = "black") + geom_line(color = "red") + theme_classic() #changed geom_line color to red p ``` ``` #using aes() in geom functions p <- ggplot(data = gapminder, aes(x=year, y=lifeExp, by = country)) p <- p geom_point(color = "black") + geom_line(aes(color = continent)) + theme_classic() #moved aes color for continent from main aes() to geom_line() p ``` ``` #create colors varialbe to select colors colors <- c('red','black','green','pink,'yellow') #manual assignment of colors p <- ggplot(data = gapminder, aes(x=year, y=lifeExp, by = country)) p <- p geom_point(color = "black") + geom_line(aes(color = continent)) + theme_classic() p <- + scale_color_manual(values = colors) # order of the colors is continets in alphabetical order p ``` ### labels for plots ``` #manual assignment of colors p <- ggplot(data = gapminder, aes(x=year, y=lifeExp, by = country)) p <- p geom_point(color = "black") + geom_line(aes(color = continent)) + theme_classic() p <- + scale_color_manual(values = colors) # order of the colors is continets in alphabetical order #creates labels for x and y p <- labs(title = "Life Expectancy over time" , x = "Year", y = "life Expectancy") p ``` ``` # adding scales p <- ggplot(data = gapminder, aes(x=year, y=lifeExp, by = country)) p <- p geom_point(color = "black") + geom_line(aes(color = continent)) + theme_classic() p <- + scale_color_manual(values = colors) # order of the colors is continets in alphabetical order #creates labels for x and y p <- labs(title = "Life Expectancy over time" , x = "Year", y = "life Expectancy") p <- p + scale_y_continuous(limits = c(0,100), breaks = seq(0,100,10)) p ``` ``` # insrtuctors code: colors <- c('red','black','green','pink,'yellow') #set variable for scale colors P <- ggplot(data = gapminder, aes(x = year, y = lifeExp, by = country)) P <- P + geom_point(color = "black") + geom_line(aes(color = continent)) + theme_classic() P <- P + scale_color_manual(values = colors) P <- P + labs(title = "Life Expectancy over Time", x = "Year", y = "Life Expectancy") P <- P + scale_y_continuous(limits = c(0,100), breaks = seq(0,100,10)) P ``` ``` P <- ggplot(data = gapminder, aes(x = year, y = lifeExp, by = country)) P <- P + geom_point(color = "black") + geom_line(aes(color = continent)) + theme_classic() P <- P + scale_color_manual(values = colors) P <- P + labs(title = "Life Expectancy over Time", x = "Year", y = "Life Expectancy") P <- P + scale_y_continuous(limits = c(0,100), breaks = seq(0,100,10)) P ``` ``` # adding scale to the plot P <- ggplot(data = gapminder, aes(x = year, y = lifeExp, by = country)) P <- P + geom_point(color = "black") + geom_line(aes(color = continent)) + theme_classic() P <- P + scale_color_manual(values = colors) P <- P + labs(title = "Life Expectancy over Time", x = "Year", y = "Life Expectancy") P <- P + scale_y_continuous(limits = c(0,100), breaks = seq(0,100,10)) P <- P + scale_x_continuous(breaks = seq(1952,2007,10)) P ``` #### subsetting plots ``` # use dplyr command to subset data dataOceania <- gapminder %>% filter(continent == "Oceania") # create inital plot and use data subset P <- ggplot(data = gapminder) P <- P + geom_line(aes(x = year, y = lifeExp, by country), color ="red") P <- P + geom_line(data = dataOceania), aes(x = year, y = lifeExp, by = country), color = "black) #plots `dataOceania` data subset P ``` ## challenge 1 1. Make a plot to highlight life expectancy over time for the country of your choice 2. Make the axis titles nice ``` #solution dataIreland <- gapminder %>% filter(country == "Ireland") P <- ggplot(data = gapminder) P <- P + geom_line(aes(x = year, y = lifeExp, by = country), color = "red") P <- P + geom_line(data = dataIreland, aes(x = year, y = lifeExp, by = country). color = "green") P ``` ## challenge 2 1. create a scatterplot of populations vs. year ``` #solution P <- ggplot(data = gapminder, aes(x = year, y = pop)) + geom_point() P ``` # #### R- Week 3 - last lessson ggplot cheatsheet: https://rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf Or alternatively, in Rstudio Help -> Cheatsheet -> ggplot2 Cheatsheet load packages and data ``` library(ggplot2) library(knitr) library(dplyr) gapminder <- read.csv("gapminder.csv") ``` ``` ## Review # ggplot() # aes() # geom_line() # color # plots as layers # everything is customizable ``` `aes()` stands for **aesthetic mapping**, which maps features of the data to visual characteristics of your plot `geom_*` specify the type of graph you want to use #### log scale ``` P <- ggplot(data = gapminder, aes(x= year, y=pop)) P <- + geom_point() P <- P + scale_y_log10() P <- labs(y = "log(pop_)") P ``` #### transparency of points ``` P <- ggplot(data = gapminder, aes(x= year, y=pop)) P <- + geom_point(alpha = 0.5) #change transparency of points P <- P + scale_y_log10() P <- labs(y = "log(pop_)") P ``` #### jitter of points ``` P <- ggplot(data = gapminder, aes(x= year, y=pop)) P <- P + geom_jitter(alpha = 0.5) #alpha changes transparency of points and add jitter to each point P <- P + scale_y_log10() P <- P + labs(y = "Log(Pop_)") P ``` For readability in a plot, when there is a bunch of points stack on top of each other # accessing help information ``` ?geom_jitter() ``` #### modifying jitter height and width ``` P <- ggplot(data = gapminder, aes(x= year, y=pop)) P <- + geom_jitter(alpha = 0.5, height = 0, width = 0.5) #alpha changes transparency of points, add jitter to each point, adding height and width to the points P <- P + scale_y_log10() P <- labs(y = "log(pop_)") P ``` #### Trendline ``` P <- ggplot(data = gapminder, aes(x= year, y=pop)) P <- + geom_jitter(alpha = 0.5, height = 0, width = 0.5) P <- P + geom_smooth(method = "lm", size = 2) #draws a linear model line, size changes width of the line P <- P + scale_y_log10() P <- labs(y = "log(pop_)") P ``` #### Box plots ## population per continent in year 2007 ``` # create subset of data gapminder2007 <- gapminder %>% filter(year == 2007) View(gapminder2007) # create plot using the data P <- ggplot(data = gapminder2007, aes(x = continent, y = pop)) P <- P + geom_boxplot() P <- P + scale_y_log10() P <- P + labs(y = "Log(Pop)") P ``` ``` P <- ggplot(data = gapminder2007, aes(x = continent, y = pop)) P <- P + geom_boxplot() P <- P + geom_jitter(height = 0, width = 0.1) #adding geom_jitter on top of the boxplot P <- P + scale_y_log10() P <- P + labs(y = "Log(Pop)") P ``` ## barplots ``` #create subset of data to for mean life expectancy meanLifeExpContinent <- gaominder %>% filter(year == 2007) %>% group_by(continent) %>% summarize(meanLifeExp + mean(lifeExp), SD(LifeExp)) View(meanLifeExpContinent) #create the barplot P <- ggplot(data = meanLifeExpContinent, aes(x = continent, y = MeanLifeExp)) P <- P + geom_col() P ``` #### Add errorbars ``` P <- ggplot(data = meanLifeContinent, aes(x = continentm y = MeanLifeExp)) P <- P + geom_col() P <- P + errorbar(aes(ymin = MeanLIfeExp - SDLifeExp, ymax = MeanLifeExp + SDLifeExp, width = 0.2)) P ``` #instructors code example up to this point ``` P <- ggplot(data = meanLifeExpContinent, aes(x = continent, y = MeanLifeExp)) P <- P + geom_col() P <- P + geom_errorbar(aes(ymin= MeanLifeExp - SDLifeExp, ymax = MeanLifeExp + SDLifeExp), width = 0.2) P ``` ### multipanel plots ``` europe <- gapminder %>% filter(continent == "Europe") P <- ggplot(data = europe, aes(x = year, y = pop)) P <- P + geom_line() P <- P + facet_wrap(~country) # create multiple panels P ``` ``` europe <- gapminder %>% filter(continent == "Europe") P <- ggplot(data = europe, aes(x = year, y = pop)) P <- P + geom_line() P <- P + facet_wrap(~country) # create multiple panels P <- P + theme(axis.text.x = element_text(angle = 45)) # adding a theme to the facet P ``` #### instructors code: ``` europe <- gapminder %>% filter(continent == "Europe") P <- ggplot(data = europe, aes(x= year, y = pop)) P <- P + geom_line() P <- P + facet_wrap(~country) P <- P + theme(axis.text.x = element_text(angle = 45)) P ``` #### Challenge Create a plot of life expectancy over year for each country in Africa ``` africas <- gapminder %>% filter(continent == "Africa") P <- ggplot(data = africas, aes(x = year, y = lifeExp)) P <- geom_line() P <- P + facet_wrap(~country) P ``` ``` SomeCountries <- c("Tunisia", "Uganda", "Zambia", "Togo") SomeCountriesData <- gapminder %>% filter(country %in% SomeCountries) ``` # Rmarkdown file menu -> new file -> rmarkdown #### cheat sheet https://rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf additional resoruce for Rmarkdown: https://rmarkdown.rstudio.com/lesson-1.html

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully