• Introduction to R
  • 1 Welcome
  • 2 What is R ?
  • 3 What is RStudio ?
    • 3.1 RStudio access
    • 3.2 RStudio interface
    • 3.3 Setting up the folder structure for the course
      • 3.3.1 Note on files and folders names
  • 4 Paths and directories
    • 4.1 Tree of directories
    • 4.2 Navigate the tree of directory with the R console
  • 5 R basics
    • 5.1 Arithmetic operators
    • 5.2 Simple calculations
    • 5.3 Objects in R
    • 5.4 Assignment operators
    • 5.5 Assigning data to an object
    • 5.6 Names of objects
  • 6 Functions
  • 7 R scripts
    • 7.1 Create and save a script
    • 7.2 R syntax
    • 7.3 RStudio tips in the console
    • 7.4 Exercice 1. Getting started ~ 15 minutes
  • 8 Data types
    • 8.1 Checking data types
  • 9 Data structures
    • 9.1 Vectors
      • 9.1.1 Creating a vector
      • 9.1.2 Vector manipulation
      • 9.1.3 Combining vectors
      • 9.1.4 Numeric vector manipulation
      • 9.1.5 Character vector manipulation
    • 9.2 Exercise 2. Numeric vector manipulation
      • 9.2.1 Exercise 2a.
      • 9.2.2 Exercise 2b.
    • 9.3 Exercise 3. Character vector manipulation
      • 9.3.1 Exercise 3a.
      • 9.3.2 Exercise 3b.
    • 9.4 Factors
    • 9.5 Matrices
      • 9.5.1 Creating a matrix
      • 9.5.2 Two-dimensional object
      • 9.5.3 Matrix manipulation
    • 9.6 Data frames
      • 9.6.1 Create a data frame
      • 9.6.2 Data frame manipulation:
    • 9.7 Two-dimensional structures manipulation
      • 9.7.1 Dimensions
      • 9.7.2 Manipulation
    • 9.8 Exercise 4. Matrix manipulation
    • 9.9 Exercise 5. Data frame manipulation
      • 9.9.1 Exercise 5a
      • 9.9.2 Exercise 5b
      • 9.9.3 Exercise 5c
  • 10 Missing values
  • 11 Input / Output
    • 11.1 On vectors
    • 11.2 On data frames or matrices
    • 11.3 Exercise 6.
      • 11.3.1 Exercise 6a. Input / output
      • 11.3.2 Exercise 6b - I/O on data frames: play with the arguments of read.table
  • 12 Library and packages
    • 12.1 R base
    • 12.2 R contrib
    • 12.3 Install a package
    • 12.4 Load a package
    • 12.5 Check what packages are currently loaded
    • 12.6 List functions from a package
    • 12.7 RStudio server at CRG
    • 12.8 Exercise 7: Library and packages
    • 12.9 Exercise (to do at home)
  • 13 Regular expressions
    • 13.1 Find simple matches with grep
    • 13.2 Regular expressions to find more flexible patterns
    • 13.3 Substitute or remove matching patterns with gsub
    • 13.4 Predefined variables to use in regular expressions:
    • 13.5 Use grep and regular expressions to retrieve columns by their names
    • 13.6 Exercise 8: Regular expressions
  • 14 Conditional statement
    • 14.1 Exercise 9: If statement
  • 15 Repetitive execution
    • 15.1 Exercise 10: For loop
  • 16 “Base” plots in R
    • 16.1 Scatter plots
  • 17 change the default palette with one of your choice:
  • 18 change the palette back to default
    • 18.1 Bar plots
    • 18.2 Pie charts
    • 18.3 Box plots
    • 18.4 Histograms
  • 19 How to save plots
    • 19.1 With R Studio
    • 19.2 With the console
    • 19.3 Exercise 11: Base plots
      • 19.3.1 Exercise 11a- scatter plot
      • 19.3.2 Exercise 11b- bar plot + pie chart
      • 19.3.3 Exercise 11c- histogram
  • 20 Plots from other packages
    • 20.1 heatmap.2 function from gplots package
    • 20.2 pheatmap function from the pheatmap package
    • 20.3 venn.diagram function from VennDiagram package
  • 21 ggplot2 package
    • 21.1 Getting started
    • 21.2 Scatter plot
    • 21.3 Box plots
    • 21.4 Dot plots
    • 21.5 Bar plots
    • 21.6 Histograms
    • 21.7 About themes
    • 21.8 Saving plots in files
    • 21.9 Exercise 12: ggplot2
      • 21.9.1 Exercise 12a- Scatter plot
      • 21.9.2 Exercise 12b- Box plot
      • 21.9.3 Exercise 12c- Bar plot
      • 21.9.4 Exercise 12d- Histogram
    • 21.10 More about the theme() function
    • 21.11 Volcano plots
  • Published with bookdown

Introduction to R

21.5 Bar plots

# A simple bar plot
ggplot(data=df2, mapping=aes(x=grouping)) + geom_bar()

  • Customize:
    • scale_x_discrete is used to handle x-axis title and labels
    • coord_flip swaps the x and y axis
# Save the plot in the object "p"
pbar <- ggplot(data=df2, mapping=aes(x=grouping, fill=grouping)) + 
  geom_bar()

pbar

# Change x axis label with scale_x_discrete and change order of the bars:
p2 <- pbar + scale_x_discrete(name="counts of yes / no", limits=c("yes", "no"))

p2

# Swapping x and y axis with coord_flip():
p3 <- p2 + coord_flip()

p3

# Change fill
p4 <- p3 + scale_fill_manual(values=c("yellow", "cyan"))

p4