R Markdown Presentation & Plotly

Riya Sutaria

30 September 2020

Introduction

In this presentation I’ll show some plots formed with plotly. For this i will be using the iris ans the EuStockMarkets data set.

#Loading the required data set and libraries.
suppressPackageStartupMessages(library(plotly))
suppressPackageStartupMessages(library(tidyr))
suppressPackageStartupMessages(library(dplyr))
data("iris")
data("EuStockMarkets")
data("diamonds")

Basic Scatterplot

plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, 
        mode = "markers", type = "scatter")

Scatter plot with color and size added to show more variables

plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, 
        color = ~as.factor(Species), mode = "markers", 
        marker = list(size = ~Petal.Length*3), type = "scatter")

3D Scatter Plot

plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, z=~Petal.Width, color =
          ~as.factor(Species),mode = "markers", marker = list(size=~Petal.Length*3),
        type = "scatter3d")
#click and move with your cursor to view in different angles.

Line Graph

#Converting the data into a data frame as plotly only takes data frame as data input.
EuStockMarket <- as.data.frame(EuStockMarkets)
plot_ly(data = EuStockMarket, x = ~time(EuStockMarkets), 
        y = ~DAX, mode = "line")
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter

Multiple Line Graph

#we'll mix up all the prices into one data frame to plot a multiline plot.
EuStockMarket <- EuStockMarket %>% gather(index, price) %>% mutate(time = rep(time(EuStockMarkets),4))
plot_ly(data = EuStockMarket, x = ~time, y = ~price, 
        color = ~index, mode = "line")
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter

Barplot

plot_ly(data = iris, x = ~Sepal.Width, type = "histogram", color = "black", colors = "000001", alpha = 0.5)

Boxplot

plot_ly(data = iris, y = ~Petal.Length, color = ~Species, type = "box")

Heatmap

mat <- matrix(rnorm(100*100), nrow = 100, ncol = 100)
plot_ly(z = ~mat, type = "heatmap")

Surface

#click and move with your cursor to view in different angles.
mat <- matrix(rnorm(100*100), nrow = 100, ncol = 100)
plot_ly(z = ~mat, type = "surface")

Surface with smoothing

#click and move with your cursor to view in different angles.
mat <- matrix(sort(rnorm(100*100)), nrow = 100, ncol = 100)
plot_ly(z = ~mat, type = "surface")

ggplot without plotly

g <- ggplot(data = diamonds, aes(x = carat , y = price)) + geom_point(aes(text = paste("Clarity :", clarity)), size = 4) + geom_smooth(aes(color = cut, fill = cut)) + facet_wrap(~cut)
## Warning: Ignoring unknown aesthetics: text
g
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

ggplot wit plotly

g <- ggplot(data = diamonds, aes(x = carat , y = price)) + geom_point(aes(text = paste("Clarity :", clarity)), size = 4) + geom_smooth(aes(color = cut, fill = cut)) + facet_wrap(~cut)
## Warning: Ignoring unknown aesthetics: text
gg <- ggplotly(g)
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
gg