1

SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

SLIDES @ https://bit.ly/rslides-9jul2020INSTALL R @ https://bit.ly/installR-9jul2020ADDITIONAL FILES* @ https://bit.ly/rfiles-9jul2020

Additional files* consist: • Installation - Powerful R Graphics.R (2 add on packages)• Powerful R Graphics.R file (Illustration during the workshop)• 4 animation files (.gif files)

Do not open these files, just download a local copy. We will explain more later.

Page 2: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Powerful R graphics

Dr. Tan Teck Kiang10:00 am – 11:30 am9 July 2020 (Thursday)

SLIDES

https://bit.ly/rslides-9jul2020

Page 3: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

House Rules

3

Please use your personal name.

Rename yourselves

(Participants hover around your name, select rename)

You will be muted by default but you may keep your

camera on

Questions during the workshop can be typed into the chat screen

Session will be recorded

Q&A at the end of the session

(email is possible)

Survey at the end of this session

Page 4: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

This workshop will be recorded and be used for NUS Libraries' archival, communications and promotional purposes. The recording will be uploaded to a secure server for access by other NUS students and staff.

By joining the event, you agree to be filmed for any and all media purposes without compensation or acknowledgement.

SLIDES

https://bit.ly/rslides-9jul2020

Page 5: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

CONTENTS

1 An Overview of R Graphics Packages2 3 General Graphical Packages in R

1 Base2 ggplot23 lattice

3 Interesting and Creative R Graphics

SLIDES

https://bit.ly/rslides-9jul2020

Page 6: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

6

WHAT IS R?

R R RR R RRR R

AN OVERVIEW OF R GRAPHICS PACKAGES

Base

lattice

ggplot2

scatterplot3d cluster

rgl

shape

AnalyzeFMRI

vcd

FactoMineR

timeSeries

forecast

circlize

plotrix

misc3d

dygraphs

The 3 General Graphical Tools

Specific Analytical Based

Page 7: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Data Frame

glm()polr()

hclust()ModelObject

plot(model)

Analytical Based

Specific

Package plot3D

3D Histogramhist3D()

Circular Visualization

Package circlize3-Dimensional Plot

Chord DiagramchordDiagram()

Page 8: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

BASE R GRAPHIC

It is available when R package is loaded

R starts up with the graphics and grDevices packages automatically loaded.

Most common to start with

Conceptually simpler

Constructed step by step through function calls.

SLIDES

https://bit.ly/rslides-9jul2020

Page 9: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

PACKAGE LATTICE

The lattice package was written by Deepayan Sarkar.

Package lattice - download as an add-on package

To use it - library(lattice)

Based on Grid Graphic System

Complex multivariate data can often be better visualized by conditioning and grouping

Most useful for conditioning types of plots: how y changes with x across levels of z

Example: Tree Circumference x Age Breakdown by Tree

Page 10: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

PACKAGE GGPLOTS

The ggplot2 package was written by Hadley Wickham.

Package gglot2 paradigm – Wilkinson’s “Grammar of Graphics” (gg)

Package ggolots2 - download as an add-on package

To use it - library(ggplot2)

Based on Grid Graphic System

Multivariate exploration is greatly simplified through faceting and colouring.

Faceting

Creating a subplots for each subset of your data

It is a good way to spot relationships.

In base R you’d have to construct each plot in a loop.

Grouping by Colours

Give different colours by group

Page 11: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Base ggplot2 lattice

plot(x,y) ggplot(aes(x,y))+ geom_point()

xyplot(y~x)

Scatter Plot

ggplot2 lattice

plot(x,y,type="l")

ggplot(aes(x,y)) + geom_line()

xyplot(y~x,type='l')

Line ChartBase

ggplot2 lattice

barplot(x) ggplot(x, y) + geom_bar()

barchart(y~x)

Bar Chart

Base

Density Plot

ggplot2 latticeBase

plot(density(x)) ggplot(aes(x)) + geom_density()

densityplot(~x)

Base, ggplot2, and lattice

Page 12: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

WHICH TO CHOOSE? BASE, LATTICE AND GGPLOT2?

Personal preferences and grammar idiosyncrasies

Presentation or exploratory?

Exploratory graphs do not have to be pretty.

Go for the plot that with minimal code – Base can be more straight forward.

Creating expository graphs for publication, intended to explain and …

All 3 works

Other add-on packages

Create for impact and diversity

ggplot and extensions add-on packages

Need comparison of a group / class

Faceting or Conditioning – Go for ggplot2 or lattice

Page 13: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

BECOME A GRAPHICAL INNOVATOR – BAR CHART

1. Faceting and Conditioning

2. Circular and Stacked Circular Bar Chart

3. Back to Back Bar Chart

4. Overlapping Bar Chart / Marimekko Replacement Chart

5. Another Circular Stacked Bar Chart

6. Rose Diagram

7. 3 Dimensional Bar Chart + Scatter Plot

Page 14: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Faceting / Conditioning

ggplot2 and lattice

Page 15: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional
Page 16: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Stacked Circular Bar Plot

Page 17: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

BACK TO BACK BAR CHART

OVERLAPPING BAR CHART

CIRCULAR BAR CHART

Page 18: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

ROSE DIAGRAM

BAR CHART

Page 19: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

3 Dimensional Bar Chart + Scatter Plot

Page 20: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

INTERESTING GRAPHS

Discrete and Categorical

Lollipop Plot

Diverging Bar Char and Lollipop Plot

Pie Chart and Waffle Plot

Scatter Pie Chart and World Map

Balloon Plot, Correspondence Analysis, Mosaic, and Alluvial Diagram

Association / Modelling

Scatterplot Matrix

Estimated Logistic Regression with Rug and Histogram

Scatter Plot with Ellipse, Text and Cluster Mean

Time Series and Longitudinal

Seasonal Subseries Plot

Heatmap for Trend

Calendar-Based Graphics

Ridgeline Plot

Map, Shape and Circle

Map

3D Plots

Circular Visualization

Animation

Time Series dygraphs()

3D Animation

Analysis of fMRI Data

Bubble Chart and Faceting

Analysis of fMRI Data

Page 21: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

DISCRETE AND CATEGORICAL

SLIDES

https://bit.ly/rslides-9jul2020

Page 22: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

A lollipop plot is basically a bar plot, where the bar is transformed in a line and a dot. It shows the relationship between a numeric and a categoric variable.Lollipop Plot

Page 23: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Diverging Bar Char and Lollipop Plot

Page 24: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Pie Chart and Waffle Plot

Page 25: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Scatter Pie Chart Scatter Pie Chart World Map

Page 26: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

WHO DO WHAT TASK? [ TABULATION TO GRAPH ]

1,744 House Task

Ask husband and wife who do the 13 tasks

Wife

Husband

Alternating

Jointly

Tabulation of 1,744 Pairs of Husband and Wife

House Task Wife Alternating Husband Jointly TotalLaundry 156 14 2 4 176Main Meal 124 20 5 4 153Dinner 77 11 7 13 108Breakfast 82 36 15 7 140Tidying 53 11 1 57 122Dishes 32 24 4 53 113Shopping 33 23 9 55 120Official 12 46 23 15 96Driving 10 51 75 3 139Finances 13 13 21 66 113Insurance 8 1 53 77 139Repairs 0 3 160 2 165Holidays 0 1 6 153 160Total 600 254 381 509 1744

Page 27: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Mosaic Plot

Correspondence Analysis

Alluvial Diagram

Balloon Plot

Page 28: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

ASSOCIATION AND MODELLING

SLIDES

https://bit.ly/rslides-9jul2020

Page 29: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Scatterplot Matrix

Page 30: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Scatterplot Matrix

Page 31: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Estimated Logistic Regression with Rug and Histogram

Page 32: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Scatter Plot with Ellipses, Text and Cluster Mean

Page 33: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

TIME SERIES AND LONGITUDINAL

Page 34: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Seasonal Subseries Plot

Page 35: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Heatmap for Trend

Polio, or poliomyelitis, is a disabling and life-threatening disease caused by the poliovirus.

Serious Symptoms

ParesthesiaFeeling of pins and needles in the legs

Meningitis Infection of the covering of the spinal cord and/or brain

ParalysisCannot move parts of the body)

Page 36: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Calendar-Based Graphics

Page 37: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Ridgeline Plot

Page 38: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

MAP, SHAPE, AND CIRCLE

Page 39: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Map

Page 40: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

3D Plots

Page 41: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Clock DartboardBagua and Taiji

Circular Visualization

Gu, Z. et al (2014). circlize implements and enhances circular visualization in R. Bioinformatics, 1-2. https://jokergoo.github.io/circlize_book/book/index.html

Chord Diagram

Page 42: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

ANIMATION

Page 43: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Time Series Package dygraphsdygraphs()

Page 44: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

3D Animation

3dAnimatedScatterplot.gifRotating Bar Chart with Scatter Plot 3D.gif

Page 45: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Animation – Bubble Chart (Population Size)Growth of Wealth and Life Expectancy by Continent

Animated Faceted Bubble Plot.gifAnimated Bubble Plot.gif

Page 46: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Analysis of fMRI Data

Page 47: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

A Lot More …

Page 48: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

BOOKS ON R GRAPHICS

Page 49: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

THANK YOU

SPECIAL THANKS TO NUS LIBRARIESDr. Magdeline Ng & Miss Kho Su Yian

FACILITATOR: Dr. Magdeline Ng

SLIDES

https://bit.ly/rslides-9jul2020

Page 50: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

HAVE A MINUTE? HELP US IMPROVEj.mp/RU_fdbk_2020

MAGDELINE NG (Dr)[email protected]

Education Services & Learning Innovation, NUS Libraries

Title: Overview of R

Page 51: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

Institute for Applied Learning Science and Educational Technology

Tan Teck Kiang

[email protected]

Thank You

Page 52: SLIDES @ INSTALL R @ https ...lib.nus.edu.sg/learning/sci/ru/session2_student.pdfThis workshop will be recorded and be used for NUS Libraries' archival, communications and promotional

SLIDES @ https://bit.ly/rslides-9jul2020INSTALL R @ https://bit.ly/installR-9jul2020ADDITIONAL FILES* @ https://bit.ly/rfiles-9jul2020

Additional files* consist: • Installation - Powerful R Graphics.R (2 add on packages)• Powerful R Graphics.R file (Illustration during the workshop)• 4 animation files (.gif files)

Do not open these files, just download a local copy. We will explain more later.