An Introduction to R Graphics

Similar documents
Intro to R for Epidemiologists

Data Visualization in R

Data Visualization Using R & ggplot2. Karthik Ram October 6, 2013

Data Visualization in R

Introduction to R for Epidemiologists

Importing and visualizing data in R. Day 3

Graphing Bivariate Relationships

data visualization Show the Data Snow Month skimming deep waters

DATA VISUALIZATION WITH GGPLOT2. Coordinates

Introduction to Graphics with ggplot2

The following presentation is based on the ggplot2 tutotial written by Prof. Jennifer Bryan.

Basics of Plotting Data

Intro to R Graphics Center for Social Science Computation and Research, 2010 Stephanie Lee, Dept of Sociology, University of Washington

An introduction to ggplot: An implementation of the grammar of graphics in R

Data Visualization. Andrew Jaffe Instructor

Stat405. Displaying distributions. Hadley Wickham. Thursday, August 23, 12

Introduction to R and Statistical Data Analysis

Statistics 251: Statistical Methods

Facets and Continuous graphs

Large data. Hadley Wickham. Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University.

DATA VISUALIZATION WITH GGPLOT2. Grid Graphics

Combo Charts. Chapter 145. Introduction. Data Structure. Procedure Options

Data Visualization. Module 7

Introduction to R for Beginners, Level II. Jeon Lee Bio-Informatics Core Facility (BICF), UTSW

The Basics of Plotting in R

Advanced Statistics 1. Lab 11 - Charts for three or more variables. Systems modelling and data analysis 2016/2017

Module 10. Data Visualization. Andrew Jaffe Instructor

03 - Intro to graphics (with ggplot2)

ggplot2 basics Hadley Wickham Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University September 2011

INTRODUCTION TO R. Basic Graphics

LondonR: Introduction to ggplot2. Nick Howlett Data Scientist

Graphics in R Ira Sharenow January 2, 2019

Statistical transformations

Package sciplot. February 15, 2013

1 The ggplot2 workflow

Visualizing Data: Customization with ggplot2

Lecture 4: Data Visualization I

Clojure & Incanter. Introduction to Datasets & Charts. Data Sorcery with. David Edgar Liebke

Chuck Cartledge, PhD. 20 January 2018

R Bootcamp Part I (B)

Boxplot

Getting started with ggplot2

Using Built-in Plotting Functions

Introduction to R: Day 2 September 20, 2017

Lab5A - Intro to GGPLOT2 Z.Sang Sept 24, 2018

Plotting Complex Figures Using R. Simon Andrews v

Install RStudio from - use the standard installation.

Plotting with Rcell (Version 1.2-5)

Rstudio GGPLOT2. Preparations. The first plot: Hello world! W2018 RENR690 Zihaohan Sang

Introduction to Data Visualization

NCSS Statistical Software

Maps & layers. Hadley Wickham. Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University.

Introduction to Statistical Graphics Procedures

Advanced Graphics in R

Graphical critique & theory. Hadley Wickham

DSCI 325: Handout 18 Introduction to Graphics in R

A set of rules describing how to compose a 'vocabulary' into permissible 'sentences'

Data Mining: Exploring Data. Lecture Notes for Chapter 3

Making Data Speak USING R SOFTWARE. 1 November 2017

Stat 849: Plotting responses and covariates

Graphics in R. Jim Bentley. The following code creates a couple of sample data frames that we will use in our examples.

Basic Statistical Graphics in R. Stem and leaf plots 100,100,100,99,98,97,96,94,94,87,83,82,77,75,75,73,71,66,63,55,55,55,51,19

DEPARTMENT OF BIOSTATISTICS UNIVERSITY OF COPENHAGEN. Graphics. Compact R for the DANTRIP team. Klaus K. Holst

Statistical Programming with R

Graphics in R STAT 133. Gaston Sanchez. Department of Statistics, UC Berkeley

Quick introduction to descriptive statistics and graphs in. R Commander. Written by: Robin Beaumont

Data analysis case study using R for readily available data set using any one machine learning Algorithm

Data Mining - Data. Dr. Jean-Michel RICHER Dr. Jean-Michel RICHER Data Mining - Data 1 / 47

An Introduction to R 2.2 Statistical graphics

STAT 1291: Data Science

A system for statistical analysis. Instructions for installing software. R, R-studio and the R-commander

Experimental Design + k- Nearest Neighbors

Error-Bar Charts from Summary Data

Econ 2148, spring 2019 Data visualization

k Nearest Neighbors Super simple idea! Instance-based learning as opposed to model-based (no pre-processing)

The diamonds dataset Visualizing data in R with ggplot2

Visualizing the World

Creating elegant graphics in R with ggplot2

Stat 849: Plotting responses and covariates

Introduction to ggvis. Aimee Gott R Consultant

Graphics in R. There are three plotting systems in R. base Convenient, but hard to adjust after the plot is created

Statistics Lecture 6. Looking at data one variable

Ggplot2 QMMA. Emanuele Taufer. 2/19/2018 Ggplot2 (1)

Package reghelper. April 8, 2017

BIOSTATS 640 Spring 2018 Introduction to R Data Description. 1. Start of Session. a. Preliminaries... b. Install Packages c. Attach Packages...

Package lvplot. August 29, 2016

Welcome to Workshop: Making Graphs

Configuring Figure Regions with prepplot Ulrike Grömping 03 April 2018

Tutorial for the R Statistical Package

PRESENTING DATA. Overview. Some basic things to remember

Model Selection Introduction to Machine Learning. Matt Gormley Lecture 4 January 29, 2018

Practical 2: Plotting

R Workshop Module 3: Plotting Data Katherine Thompson Department of Statistics, University of Kentucky

Package superheat. February 4, 2017

BL5229: Data Analysis with Matlab Lab: Learning: Clustering

Mixed models in R using the lme4 package Part 2: Lattice graphics

Data Mining: Exploring Data. Lecture Notes for Chapter 3

Az R adatelemzési nyelv

k-nearest Neighbors + Model Selection

Data Mining: Exploring Data. Lecture Notes for Chapter 3. Introduction to Data Mining

Transcription:

An Introduction to R Graphics PnP Group Seminar 25 th April 2012

Why use R for graphics? Fast data exploration Easy automation and reproducibility Create publication quality figures Customisation of almost every aspect of the plot You can feel smug when people still use Excel

A quick recap on vectors and data frames Combine for vectors: c() c(1, 6, 4, 7, 9, 4, 2) c( A, B, C, D ) # integers # factors Reading in a data frame from a text file: unicorns<-read.table( unicorns.txt, header=true) Calling a variable from a data frame using $ > head(unicorns) birthweight sex longevity 1 4.478424 Male 1 2 5.753458 Male 0 3 3.277265 Male 0 4 3.929379 Male 0 To get birth weight alone: unicorns$birthweight

Getting help in R For more information on any command in R, use the question mark! >?plot Use?? to search all of the help documents >??boxplot Use the tab key in RStudio

Overview Part 1: Base graphics Graphical tools already included in R Simple, fast, exploratory graphics Important to know Part 2: ggplot2 More complex, higher quality graphics Fashionable to know Easier to master?

1. Basic Histogram 2. Line Graph with Regression 3. Scatterplot with Legend 4. Boxplot with reordered/ formatted axes 5. Boxplot with Error Bars

See script Data for Graphs: A Reminder

1. hist function: Histogram Visual representation of data distribution Birth weight and longevity in unicorns

1. Basic Histogram ylim main breaks ylab xlab xlim

2: plot function: Basic line graph with regression Moomins are a common pest species in Finland Data on population density in Ruissalo from 1971-2000

2. Basic line graph with regression text() abline(fit1) type lwd col

3. plot function: Scatterplot with legend Iris setosa Iris versicolor Iris virginica Measured sepal length, sepal width, petal length and petal width in three species of Iris (RA Fisher and ER Anderson). Quantify the morphological variation in the three related species Dataset is included in R

3. plot function: Scatterplot with legend col pch

3. Scatterplot with legend legend() col pch cex

4. boxplot function Continue with the same dataset

4. boxplot function las cex.main cex.lab notch cex.axis

5. barplot function Dragons are commonly studied in the United Kingdom Extensive data on talon length in three countries.

5. barplot function

What are the limitations of base graphics? Just the tip of the iceberg You could feasibly do anything you require in base graphics, but Some common actions are not straightforward Legends Dodged plots Faceting (lattice) Error Bars (gplots) Formatting axes and plot area Complex graphs are time-consuming. My advice Base graphics best for quick and dirty exploratory graphics ggplot2 is best for everything else

Overview Part 1: Base graphics Simple, fast, exploratory graphics Important to know Part 2: ggplot2 More complex, higher quality graphics Fashionable to know Easier to master

Library ggplot2 Created by Hadley Wickham: had.co.nz/ggplot2/

ggplot2 uses three components to construct a graph. 1. Layers: data with aesthetic properties Data: iris Aesthetic properties of the data x = Sepal.Length, y = Petal.Length, colour = Species 2. Geoms: the type of plot you make. a line graph, a scatterplot, a boxplot 3. Stats: statistical transformations e.g. assigning data to bins, smoothing lines, etc. Every geom has a default statistic, so this is not always specified. This should become clearer as we go along.

Overview 1. Learn how to build plots Data + aesthetic values Geoms Stats Faceting and Dodging 2. Customise the layout

1. Histogram ggplot(iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 0.5, col = "black", fill = "purple") DATA GEOM

2. Line Graph ggplot(moomins, aes(x = Year, y = PopSize)) + stat_smooth(method = "lm", col = "black", se = F) + geom_line(col = "red", size = 2) DATA STAT GEOM

3. Scatterplot ggplot(iris, aes(x=sepal.length, y=petal.length, col=species)) + stat_smooth(method = "lm") + geom_point(size = 4) DATA STAT GEOM

4. Boxplot ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot(width=0.6) + geom_jitter(position=position_jitter(width=0.1), aes(col = Species)) DATA GEOM GEOM

5. Barplot ggplot(dragons, aes(x = Population, y = TalonLength)) + geom_bar(fill = "slateblue") + geom_errorbar(aes(ymax = TalonLength + SE, ymin = TalonLength - SE), width = 0.2) DATA GEOM GEOM

6. Faceting ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) + geom_point() + stat_smooth(method="lm") + facet_wrap(~ Species, scales = "free")

7. Dodging ggplot(dragons2, aes(x = Population, y = TalonLength, fill = Sex)) + geom_bar(position = position_dodge(width = 0.9)) + geom_errorbar(position = position_dodge(width = 0.9), aes(ymax = TalonLength + SE, ymin = TalonLength - SE),width = 0.2)

Customisation ggplot(dragons2, aes(x = Population, y = TalonLength, fill = Sex)) + geom_bar(position = position_dodge(width = 0.9)) + geom_errorbar(position = position_dodge(width = 0.9), aes(ymax = TalonLength + SE, ymin = TalonLength - SE),width = 0.2)

Where can I get help for R Graphics? R-Bloggers: r-bloggers.com/ Stack Overflow: stackoverflow.com/ R mailing lists archive: tolstoy.newcastle.edu.au/r/ ggplot2 documentation: had.co.nz/ggplot2/ R colour chart: research.stowers-institute.org/efg/r/color/chart/colorchart.pdf