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 https://www.ecoccs.com/rtraining.html {R Trainings and Resources provided by EcoC2S (Irucka Embry, EIT)}

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 (https://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("https://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")

# using ggplot from the ggplot2 package
p <- ggplot(data = mydata, aes(x = Year, y = pH)) + geom_point(shape = 1, size = 2)
print(p)

# ?ggplot # retrieve R help on the command
# ?aes # retrieve R help on the command
# ?geom_point # retrieve R help on the command
# ?print # retrieve R help on the command



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

attach(mydata2)

# change the column names
names(mydata2) <- c("2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013")


# Code for Notched Box Plot
# using base boxplot
boxplot(mydata2, notch = TRUE,

# Gives the title and axis names
main = "Mammoth Cave National Park Rainfall pH", xlab = "Year", ylab = "pH",

# Sets the colors
col = (c("gold","darkgray", "darkorchid1", "cyan", "white", "red","limegreen", "magenta", "chartreuse1", "hotpink1")))

# Adds the line at 18.5
abline(a = NULL, b = NULL, h = 18.5, v = NULL, reg = NULL, coef = NULL, untf = FALSE)

# ?boxplot # retrieve R help on the command
# ?abline # retrieve R help on the command


# Code for Violin plot
library(vioplot)
vioplot(mydata$pH, col = "blue")
title(main = "Violin plot MCNP Rainfall pH", xlab = "", ylab = "pH")

# Code for beanplot
library(beanplot)
beanplot(mydata$pH, col = "blue")
title(main = "beanplot MCNP Rainfall pH", xlab = "", ylab = "pH")



R Resources pages

https://www.ecoccs.com/rtraining.html
R Trainings and Resources provided by EcoC2S (Irucka Embry, EIT)



Resources used for this Tutorial

Source 1
https://web.archive.org/web/20150310074459/http://usepa.github.io/introR/ [Recovered with the Internet Archive: Wayback Machine]
USEPA Introduction To R
Jeff Hollister


Source 2
https://sites.google.com/site/davidsstatistics/real-world-studies/mcnp-rainfall-ph
MCNP-Rainfall-pH
David's Statistics
Last update April 21, 2014



Donations accepted with Liberapay

If you would like to contribute to the continued development of Irucka Embry’s R packages and/or Irucka Embry’s R Examples, please feel free to donate via the link below:

Please feel free to review Irucka Embry (iaembry)’s profile on Liberapay.