## Getting help help("mean") ## or ?mean # for a particular package vignette("extending-ggplot2") vignette("phyloseq-basics") ## Calculator 2+2 ## Assigning a value to a variable a <- 2*4 ## or a = 2*4 (the # sign signals a comment) a a/2 a ## Changing the value of a variable a <- 4 a b <- a; b <- 8 a; b ## Elementary modes in R ## ----numeric------------------------------------------------------------- x <- 1 class(x) ## ----character----------------------------------------------------------- x <- "hello" class(x) ## ----logical------------------------------------------------------------- x <- TRUE ## or FALSE class(x) ## Special values a <- NA; length(a); is.na(a) ############# ## VECTORS ## ############# ## ----c-command----------------------------------------------------------- x <- c(2, 4, 8, 9, 0) x ## prints x ## ----subsetting-vector-1------------------------------------------------- x[1] ## first element ## ----subsetting-vector-2------------------------------------------------- x[c(3, 5)] ## third and fifth elements ## ----coercion-vector-1--------------------------------------------------- c(3.4, 2, TRUE) ## ----coercion-vector-2--------------------------------------------------- c(3.4, "MaIAGE", TRUE) ## ----named-vector-------------------------------------------------------- x <- c("A" = 1, "B" = 4, "C" = 9) x ## ----named-vector-1------------------------------------------------------ x[1] ## ----named-vector-2------------------------------------------------------ x["C"] ## ----named-vector-3------------------------------------------------------ x <- c(1, 4, 9) x names(x) <- c("first", "second", "third") x ## Try to guess the result x <- c("O", "G", "F", "S", "R") x[c(3, 5, 1, 2, 4)] ## ----logical-indexing-1-------------------------------------------------- x <- 1:6 x index <- c(TRUE, FALSE, TRUE, TRUE, FALSE, FALSE) x[index] ## = x[c(1, 3, 4)] ## ----logical-indexing-2-------------------------------------------------- index <- c(TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE) x[index] ## = x[c(1, 3, 4, 7)] but x[7] does not exist ## Exercice rank <- c("Order", "Kingdom", "Genus", "Class", "Family", "Species", "Phylum") # Create a new_rank vector based on rank in the correct order reordered_rank <- rank[c(2, 7, 4, 1, 5, 3, 6)] ############ ## MATRIX ## ############ ## ----matrices------------------------------------------------------------ x <- matrix(1:18, nrow = 3, ncol = 6) ## compare to x <- matrix(1:18, nrow = 3, ncol = 6, byrow = TRUE) x x[2, 4] ## element in 2nd row, 4th column ## ----indexing-matrix-1--------------------------------------------------- x[, 2] ## 2nd column ## ----indexing-matrix-2--------------------------------------------------- x[2, ] ## 2nd row ## Try to guess the result x <- as.matrix(read.csv("data/introR/matrix.tsv", row.names=1, sep="\t")) x x[ , 3] x["otu_2",] x[c(1, 2), ] x[c(1, 3), c(2, 3)] ## How to access to the count of sample2 for otu_3 ? x["otu_3", "sample2" ] x["otu_3", 2 ] x[3, 2 ] x[3, "sample2" ] ############ ## FACTOR ## ############ ## ----factor-example------------------------------------------------------ x <- factor(c("male", "male", "female", "male", "female")) class(x) ## ----levels-------------------------------------------------------------- levels(x) ## ----relevel------------------------------------------------------------- x <- as.factor(c("strong", "strong", "weak", "middle", "weak")) levels(x) y <- factor(x, c("weak", "middle", "strong")) levels(y) ################ ## DATA FRAME ## ################ ## ----data.frame, echo = -1----------------------------------------------- x <- data.frame(number = c(1:4), group = factor(c("A", "A", "B", "B")), desc = c("riri", "fifi", "lulu", "picsou")) ## Characteristics of x x class(x) class(x[,1]) class(x[,2]) ## ----data.frame----------------------------------------------- x <- data.frame(number = c(1:4), group = factor(c("A", "A", "B", "B")), desc = c("riri", "fifi", "lulu", "picsou"), stringsAsFactors = FALSE) x[2, "desc"] ## or x[2, 3] dim(x); nrow(x); ncol(x) x$group ## Exercice setup x <- data.frame(ID = 1:10, group = rep(c("A", "B"), each = 5), value = rnorm(10)) x ## Try to guess the result ii <- 1:5 df <- x[ii, c("ID", "value")] df df[ , 2] class(df[ , 2]) df[2, ] class(df[2, ]) ############################# ## BUILDING LOGICAL FILTER ## ############################# x <- 11:15 x z <- (x < 13); z ## the first command returns a logical vector z <- (x < 14) & (x > 11); z ## logical AND z <- (x < 12) | (x > 14); z ## logical OR !z ## logical not which(z) z <- which(x < 14) x[z] x[x < 14] ############# ## GGPLOT2 ## ############# ## ----load-ggplot, cache = FALSE------------------------------------------ library(ggplot2) ## ----diamonds------------------------------------------------------------ data(diamonds) ## import datasets class(diamonds) head(diamonds) ## first line of data.frame help(diamonds) ## ----ggplot-first-example------------------------------------------------ ## set base plot, x coordinate is carat, y is price p <- ggplot(diamonds, aes(x = carat, y = price)) ## Add a layer to represent data as point p1 <- p + geom_point() plot(p1) ## ----ggplot-color-example------------------------------------------------ ## set base plot, x coordinate is carat, y is price colored by cut p <- ggplot(diamonds, aes(x = carat, y = price, color=cut)) ## Add a layer to represent data as point p2 <- p + geom_point() plot(p2) ## OR p <- ggplot(diamonds, aes(x = carat, y = price)) ## Add a layer to represent data as point p2 <- p + geom_point(aes(color=cut)) plot(p2) ## ----ggplot-boxplot-example------------------------------------------------ ## Add a layer to represent data as boxplot p <- ggplot(diamonds, aes(x = cut, y = price)) p3 <- p + geom_boxplot() plot(p3) # colored by clarity p4 <- ggplot(diamonds, mapping = aes(x = cut, y = price, color=clarity)) + geom_boxplot() plot(p4) ########## FACETTING ## ----facet_wrap, fig.width = 10, fig.height = 4-------------------------- ## facet along cut p5 <- p2+ facet_wrap(~ cut) plot(p5) ## ----facet_grid, fig.width = 10, fig.height = 2.5------------------------ ## facet along cut, only points from a given cut appear in a facet p6 <- p2 + facet_grid(~ cut) plot(p6) ## ----facet_grid-2-factors, fig.width = 10, fig.height = 5, size = "tiny"---- ## facet along clarity (rows) * cut (column) p7 <- p2 + facet_grid(clarity ~ cut) plot(p7) ## On boxplot p8 <- p4 + facet_wrap(~cut) plot(p8) ## free x p9 <- p4 + facet_wrap(~cut, scales="free_x") plot(p9) ## ----palette------------------------------------------------------------- palette <- c("black", "red", "blue", "magenta", "gray") names(palette) <- c("Fair", "Good", "Very Good", "Premium", "Ideal") ## ----second-example-1, size = "tiny", fig.height = 4.5------------------- ## Manual color scale p6.1 <- p6 + scale_color_manual(values = palette) plot(p6.1) ## ----second-example-2, size = "tiny", fig.height = 4.5------------------- ## Use built-in color palette p7.1 <- p7 + scale_color_brewer() plot(p7.1) ## Add title p10 <- p9 + ggtitle("Diamond prices as a function of clarity") + xlab("Diamond clarity") + ylab("Diamond price") plot(p10) ## Add title (center) p10 <- p9 + ggtitle("Diamond prices as a function of clarity") + xlab("Diamond clarity") + ylab("Diamond price")+ theme(plot.title = element_text(hjust = 0.5)) plot(p10) ################ ## GO FURTHER ## ################ ################# ## CONVERSIONS ## ################# ## Examples as.integer("5") as.logical(0.0) as.numeric(TRUE) as.character(TRUE) as.numeric("5.56") as.logical(2) as.integer("INRA") ## Try to guess the result: integer as.integer(2/3) as.integer(5.67) as.integer(FALSE) as.integer(TRUE) as.integer("5.67") as.integer("MaIAGE") ## Try to guess the result: numeric as.numeric(2/3) as.numeric(5.67) as.numeric(FALSE) as.numeric(TRUE) as.numeric("5.67") as.numeric("MaIAGE") ## Try to guess the result: character as.character(2/3) as.character(5.67) as.character(FALSE) as.character(TRUE) as.character(5) as.character(5+7) ## Try to guess the result: logical as.logical(2/3) as.logical(0) as.logical("45") as.logical("MaIAGE") ## Guess the result : TRUE + TRUE + FALSE * TRUE + TRUE * TRUE ## ----converting-vector-1------------------------------------------------- as.numeric(x) ## 1="a", 2="b" as.character(x) ## ----converting-vector-2------------------------------------------------- as.integer(y) ## 1="b", 2="a" as.character(y)