20
Introduction to Interactive Plots **************** Osama Mahmoud *** Website: http://osmahmoud.com E-mail: [email protected] ***** 15 Novemvber 2019

IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Introduction to Interactive Plots

****************Osama Mahmoud

***Website: http://osmahmoud.comE-mail: [email protected]

*****15 Novemvber 2019

Page 2: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Goals

In this sesseion, we will:

Introduce interactive plots in R.

Show how to use plotly in R to produce basic interactive graphics.

Show useful extension of ggplot2 and plotly for visualisingcorrelation matrices.

Demonstrate how to save your interactive plots in html format.

Osama Mahmoud Session 09: Interactive Plots 1 / 19

Page 3: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Contents

1 Introduction to data visualisation

2 Overview of ggplotly

3 Correlation matrices

Osama Mahmoud Session 09: Interactive Plots 2 / 19

Page 4: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Install and load required packages

Installing packages in R is straightforward. For example:> install.packages(c("ggcorrplot", "plotly"))

Then, you can simply load it to your R session whenever needed:> library("plotly"); library("plotly")

Osama Mahmoud Session 09: Interactive Plots 3 / 19

Page 5: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Produce interactive plotsusing ggplotly

Osama Mahmoud Session 09: Interactive Plots 4 / 19

Page 6: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Histograms

> g = ggplot(data=movies, aes(x=year)) + geom_histogram(binwidth = 1,fill="#2b8cbe", alpha=0.6)

> g + xlab("Year") + ylab("Number of movies produced") + theme_bw()

0

500

1000

1500

2000

1890 1920 1950 1980 2010Year

Num

ber o

f mov

ies

prod

uced

> ggplotly(g)

Osama Mahmoud Session 09: Interactive Plots 5 / 19

Page 7: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Density

> g = ggplot(movies, aes(x=rating)) + geom_density(fill="#2b8cbe",alpha=0.6)

> g + ylab("Density of Movie Rating") + xlab("Score (out of 10)")

0.0

0.1

0.2

2.5 5.0 7.5 10.0Score (out of 10)

Den

sity

of M

ovie

Rat

ing

> ggplotly(g)

Osama Mahmoud Session 09: Interactive Plots 6 / 19

Page 8: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

box-plot

> g = ggplot(movies, aes(x=factor(cat_length), y=rating)) +xlab("length") + ylab("Rating (out of 10")

> g + geom_boxplot(fill="red", alpha=0.4)

2.5

5.0

7.5

10.0

(0,30]

(30,60]

(60,75]

(75,90]

(90,105]

(105,120]

(120,Inf]

length

Rat

ing

(out

of 1

0

> ggplotly(g)

Osama Mahmoud Session 09: Interactive Plots 7 / 19

Page 9: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

violin> g = g + geom_violin(fill="red", alpha=0.4) + stat_summary(fun.y =median, geom=’point’)

> g + theme_bw() + theme(axis.text=element_text(face=’bold’, size =

12, angle = 45, hjust = 1))

●●

2.5

5.0

7.5

10.0

(0,30]

(30,60]

(60,75]

(75,90]

(90,105]

(105,120]

(120,Inf]

length

Rat

ing

(out

of 1

0

> ggplotly(g)Osama Mahmoud Session 09: Interactive Plots 8 / 19

Page 10: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Scatter plot

> data(mpg, package = "ggplot2")> g = ggplot(mpg, aes(displ, hwy, colour=class)) + geom_point()

> g + scale_color_manual(values=rainbow(7))

●●

●●

●● ●●

● ●

● ●

●●

●●

● ●

●●

●●

● ●

●●

● ●

● ●

●●●

●●

●●

●●

●●

●●●

● ●

● ●

●●

● ●

●●

● ●

●●

●● ●●

●●

●●

●●

●●

●●

●●

●●

●● ●●

●●●

●● ●

20

30

40

2 3 4 5 6 7displ

hwy

class

2seater

compact

midsize

minivan

pickup

subcompact

suv

> Fig = ggplotly(g)

Osama Mahmoud Session 09: Interactive Plots 9 / 19

Page 11: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Saving interactive plots

> require(htmlwidgets)> htmlwidgets::saveWidget(widget = Fig, "Scatter_plot.html")

Osama Mahmoud Session 09: Interactive Plots 10 / 19

Page 12: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Prepare your dataCompute the correlation matrix> data(mtcars); corr.mat <- round(cor(mtcars), 1)Compute correlation P-values> pval.cor <- cor_pmat(mtcars)Visualize the correlation matrix: method = "square" (default)> (G1 = ggcorrplot(corr.mat))

mpg

cyl

disp

hp

drat

wt

qsec

vs

am

gear

carb

mpg cy

ldis

p hp drat wt

qsec vs am ge

arca

rb

−1.0

−0.5

0.0

0.5

1.0Corr

Osama Mahmoud Session 09: Interactive Plots 11 / 19

Page 13: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Reordering the correlation matrix using hierarchicalclustering> G2 = ggcorrplot(corr.mat, hc.order = TRUE)Interactive form> ggplotly(G2)

carb

wt

hp

cyl

disp

qsec

vs

am

gear

mpg

drat

carb wt

hp cyl

disp

qsec vs am ge

arm

pg drat

−1.0

−0.5

0.0

0.5

1.0Corr

Osama Mahmoud Session 09: Interactive Plots 12 / 19

Page 14: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Correlation matrix: Lower triangle

> G3 = ggcorrplot(corr.mat, hc.order = TRUE, type = "lower")Interactive form> ggplotly(G3)

carb

wt

hp

cyl

disp

qsec

vs

am

gear

mpg

wthp cy

ldis

pqs

ec vs am gear

mpg dr

at

−1.0

−0.5

0.0

0.5

1.0Corr

Osama Mahmoud Session 09: Interactive Plots 13 / 19

Page 15: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Correlation matrix: Upper triangle

> G4 = ggcorrplot(corr.mat, hc.order = TRUE, type = "upper")Interactive form> ggplotly(G4)

wt

hp

cyl

disp

qsec

vs

am

gear

mpg

drat

carb wt

hp cyl

disp

qsec vs am ge

arm

pg

−1.0

−0.5

0.0

0.5

1.0Corr

Osama Mahmoud Session 09: Interactive Plots 14 / 19

Page 16: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Correlation matrix: Add coeficients

> G5 = ggcorrplot(corr.mat, hc.order = TRUE, type = "lower", lab =TRUE)Interactive form> ggplotly(G5)

0.4 0.7 0.5 0.4 −0.7 −0.6 0.1 0.3 −0.6 −0.1

0.7 0.8 0.9 −0.2 −0.6 −0.7 −0.6 −0.9 −0.7

0.8 0.8 −0.7 −0.7 −0.2 −0.1 −0.8 −0.4

0.9 −0.6 −0.8 −0.5 −0.5 −0.9 −0.7

−0.4 −0.7 −0.6 −0.6 −0.8 −0.7

0.7 −0.2 −0.2 0.4 0.1

0.2 0.2 0.7 0.4

0.8 0.6 0.7

0.5 0.7

0.7

carb

wt

hp

cyl

disp

qsec

vs

am

gear

mpg

wthp cy

ldis

pqs

ec vs am gear

mpg dr

at

−1.0

−0.5

0.0

0.5

1.0Corr

Osama Mahmoud Session 09: Interactive Plots 15 / 19

Page 17: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Correlation matrix: Add significance level> G6 = ggcorrplot(corr.mat, hc.order = TRUE, type = "lower", p.mat =pval.cor, sig.level = 0.01, insig = "pch", pch = 4, pch.col ="black")Interactive form> ggplotly(G6)

carb

wt

hp

cyl

disp

qsec

vs

am

gear

mpg

wthp cy

ldis

pqs

ec vs am gear

mpg dr

at

−1.0

−0.5

0.0

0.5

1.0Corr

Osama Mahmoud Session 09: Interactive Plots 16 / 19

Page 18: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Correlation matrix: Change theme and save plot> G7 = ggcorrplot(corr.mat, hc.order = TRUE, type = "lower",outline.col = "white", ggtheme = ggplot2::theme_bw(), colors =c("darkgreen", "white", "blue"))Interactive form> G7 = ggplotly(G7)

carb

wt

hp

cyl

disp

qsec

vs

am

gear

mpg

wthp cy

ldis

pqs

ec vs am gear

mpg dr

at

−1.0

−0.5

0.0

0.5

1.0Corr

Osama Mahmoud Session 09: Interactive Plots 17 / 19

Page 19: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Saving interactive plots

Save the previous plot (Interactive form)> htmlwidgets::saveWidget(widget = G7, "corr.html")

Osama Mahmoud Session 09: Interactive Plots 18 / 19

Page 20: IntroductiontoInteractivePlots · Introduce interactive plots in R. ... 1 Introduction to data visualisation 2 Overview of ggplotly 3 Correlation matrices Osama Mahmoud Session 09:

Interactive plots Introduction ggplotly Correlation matrices

Thank You

Osama Mahmoud Session 09: Interactive Plots 19 / 19