Irucka Embry, EIT [Cherokee Nation Technology Solutions (CNTS) United States Geological Survey (USGS) Contractor] gave this tutorial to his USGS colleagues on Friday, 27 March 2015. This tutorial has been modified from its original presentation.



Pre-Tutorial

We will start by checking out http://www.ecoccs.com/RandUSGS.html (R Resources provided by Irucka Embry)

That Web page contains information related to the R Tutorial as well as many other useful resources

Everyone should have a copy of R for MATLAB users (http://mathesaurus.sourceforge.net/octave-r.html). This document is useful for showing R commands and a description of the commands.



Tutorial

This tutorial is focused on visualizing data in R



Table of Contents

get -> clean -> explore -> VISUALIZE -> analyze (Source 1)

NOTE: Prior to beginning this R Tutorial, it is advised that you have already downloaded and installed R 3.3.1 (if using Microsoft Windows, then download and install the R binary file from https://cloud.r-project.org/bin/windows/base) and RStudio for your operating system.


Installing required R packages (and their dependencies) for the R Tutorial

If you are unsure if you have all of the packages already installed

# install & load the packages
install.packages("install.load")
install.load::install_load("data.table", "ggplot2", "directlabels", "ggthemes", "scales", "GGally", "vioplot", "beanplot")
# Please note that many package dependencies will also be installed in the process of installing the packages in this list


If you are sure that you have all of the packages installed

# install & load the packages
# install.packages(install.load) # install the install.load package maintained by Irucka Embry
install.load::load_package("data.table", "ggplot2", "directlabels", "ggthemes", "scales", "GGally", "vioplot", "beanplot") # load the packages and dependencies



If you are not in your working directory and you would like to either import or export a file, then you will need to make sure that the pathname can be read by R

For example, if you want to read “mammals.exp” from “C:.exp”, then in R you would change the  to / so “C:/Documents/mammals.exp” is CORRECT in R



get -> clean -> explore -> VISUALIZE -> analyze

VISUALIZE

The following is a slightly modified version of Source 2

install.load::load_package("data.table", "ggplot2", "directlabels", "ggthemes", "scales", "GGally", "vioplot", "beanplot") # load the packages and dependencies

# Load data
mydata <- read.csv("http://www.ecoccs.com/R_Tutorial/27_March_2015/pH-data.csv", header = TRUE)
attach(mydata)

# ?attach # retrieve R help on the command

# change the column names
names(mydata)[6:ncol(mydata)] <- c("2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013")

# ?names # retrieve R help on the command


# Code for Scatter Plot
# using base plot

plot(Year, pH)

# ?plot # retrieve R help on the command


# using qplot (quick plot) from the ggplot2 package
library(ggplot2)
# ?qplot # retrieve R help on the command

qplot(mydata$Year, mydata$pH, xlab = "Year", ylab = "pH")