class: center, middle, inverse, title-slide .title[ # An Interactive Journey through Dynamic Data Landscapes ] .subtitle[ ## Presentation (GROUP-4) ] .author[ ### Jokhrof Ahmed Doha
Niaz Rahman Kamiub
Samia Sultana Reti
Mst. Suchurita
Md. Yaqub Islam ] .institute[ ### ISRT (Applied Statistics) ] .date[ ### (2024-02-03) ] --- class: center, middle, inverse # plotly --- <br><br><br> **plotly** is a data visualization library that allows users to create interactive and visually appealing graphs and charts in various programming languages. <br><br><br> It supports languages such as R, Python and Javascipt. <br> <br> <br> Key features: - Open source - Has an online platform - Provides interactivity <br><br><br> --- ##Basic scatter plot using **ggplot2** .pull-left[ ```r mtcars %>% ggplot( aes( x = mpg, y = disp))+ geom_point(size = 2)+ labs(title = "Scatter plot", x = "Miles per gallon", y = "Displacment")+ theme_bw(base_size = 14) ``` ] .pull-right[ <!-- --> ] --- ##Creating scatter plot using **plotly**: ```r plot_ly( data = mtcars, x=~mpg, y=~disp, type = "scatter", mode = "markers", height = 300, width = 700) ```
Note : If type is not specified, plotly will choose the best possible plot according to the inputs. --- ##Add color: ```r plot_ly( data = mtcars, x=~mpg, y=~disp, type = "scatter", mode = "markers", color = I("black"), height = 330, width = 700) ```
--- ##Aesthetic mapping: ```r plot_ly( data = mtcars, x=~mpg, y=~disp, type = "scatter", mode = "markers", color = ~as.factor(gear), symbol = ~as.factor(cyl), symbols = c('circle','x','o'), marker = list(size = 6), height = 330, width = 700) ```
--- ##Hide Legend: ```r plot_ly( data = mtcars, x = ~mpg, y = ~disp, type = "scatter", mode = "markers", symbol = ~as.factor(cyl), symbols = c('circle','x','o'), marker = list(size = 6), height = 330, width = 700) %>% layout(showlegend = FALSE) ```
--- ##Naming axis and title: We can name axis and title using the **layout()** function. <br><br><br> ```r plot_ly( data = mtcars, x=~mpg, y=~disp, type = "scatter", mode = "markers", symbol =~ as.factor(cyl), symbols = c('circle','x','o'), marker = list(size = 6), height = 500, width = 700 ) %>% layout( title = "scatter plot using plotly", xaxis = list(title = "Miles Per Gallon", showgrid = FALSE), yaxis = list(title = 'Displacment', showgrid = FALSE) ) ``` ---
--- ##MOUSE HOVER TEXT: .pull-left[ ```r plot_ly( data = mtcars , x = ~mpg, y = ~disp, type = "scatter", mode = "markers", color = I("black"), hoverinfo = "text", text = paste( "miles per gallon :", mtcars$mpg, "<br>", "disp :", mtcars$disp) ) ``` ] .pull-right[
] --- ##Add annotation: ```r plot_ly( data = mtcars , x = ~mpg, y = ~wt, type = "scatter", mode = "markers", hoverinfo = "text", text = paste("miles per gallon :", mtcars$mpg, "<br>", "weight :", mtcars$wt ) )%>% add_annotations( x = mtcars$mpg[which.max(mtcars$mpg)], y = mtcars$wt[which.max(mtcars$mpg)], text = "good miles", font = list(size = 15) ) ``` ---
--- ##Adding data source <br> ```r plot_ly( data = mtcars , x = ~mpg, y = ~wt, type = "scatter", mode = "markers", hoverinfo = "text", text = paste("miles per gallon:", mtcars$mpg, "<br>", "weight:", mtcars$wt) ) %>% add_annotations( xref = "paper", yref = "paper", x = .8, y = .8, text = "Data source :'mtcars'", font = list(size = 20), showarrow = FALSE ) ``` ---
--- ##Multiple annotation: ```r plot_ly( data = mtcars , x = ~mpg, y = ~wt, type = "scatter", mode = "markers" )%>% add_annotations( x = mtcars$mpg[which.max(mtcars$mpg)], y = mtcars$wt[which.max(mtcars$mpg)], text = "high miles", font = list(size = 15)) %>% add_annotations( x = mtcars$mpg[which.min(mtcars$mpg)], y = mtcars$wt[which.min(mtcars$mpg)], text = "low miles", font = list(size = 15)) ``` ---
--- class: center, middle, inverse Animated plot with <br> # ggplot and plotly --- ```r gapminder %>% plot_ly( x = ~gdpPercap, y = ~lifeExp, size = ~pop, color = ~continent, frame = ~year, text = ~country, hoverinfo = "text", type = 'scatter', mode = 'markers' ) %>% layout( xaxis = list( type = "log"), font = list(size = 13) ) %>% animation_opts( frame = 1000, easing = "elastic", redraw = FALSE ) %>% animation_button( x = 0, xanchor = "left", y = -0.5, yanchor = "bottom" ) %>% animation_slider( currentvalue = list(prefix = "YEAR ", font = list(color="red")) ) ``` ---
--- ##Animated Line Plot ```r dat <- babynames %>% filter(name %in% c("Patricia", "Helen","Elizabeth")) %>% filter(sex == "F") dat %>% ggplot( aes(x = year, y = n, group = name, color = name) ) + geom_line(size = 2) + geom_point(size = 4) + ggtitle("Popularity of American names in the previous 30 years") + theme_bw(base_size = 18) + ylab("Number of babies born") + transition_reveal(year) ``` --- <img src="44_files/figure-html/large-plot-out-1.gif" width="100%" /> --- Animated boxplot:<br><br><br> We can make animated plots using **plotly**. ```r gapminder %>% plot_ly( x = ~continent, y = ~lifeExp, type = "box", color = ~continent, frame = ~year) %>% layout(title = "Interactive boxplot with plotly", xaxis = list(title = "Continents"), yaxis = list(title = "Life Expectancy"), height = 600, width = 800 ) ``` ---
--- class: center, middle ### ggplotly() --- **ggplotly()** is a function which converts a ggplot to an interactive plotly plot. The function of the **plotly** package. <br><br><br> .pull-left[ ```r p1 <- mtcars %>% ggplot()+ geom_point(aes(x = wt, y = mpg), col = "steelblue") ggplotly(p1) %>% layout(height = 400, width = 400) ``` ] .pull-right[
] --- ### Interactive probability distributions: Binomial distribution: ```r dat_binom <- tibble(x = 0:10, prob = dbinom(x = x, size = 10, prob = .5), cprob = pbinom(q = x, size = 10, prob = .5)) ``` .pull-left[ ```r p1<-ggplot(data = dat_binom)+ geom_col(aes(x = x, y = prob), fill = "red") ggplotly(p1) %>% layout(width = 400, height =360, title = "Binomial Plot", xaxis = list(title = "X"), yaxis = list(title = "Binomial Probability"), font = list(size = 12)) ``` ] .pull-right[
] --- Cumulative binomial probability distribution: ```r p2 <- ggplot(data = dat_binom) + geom_step(aes(x = x, y = cprob), col = "darkgreen") + scale_x_continuous(breaks = 0:10) ggplotly(p2) %>% layout(width = 750, height =340, title = "Cumulative Binomial Plot", xaxis = list(title = "X"), yaxis = list(title = "Cumulative Binomial Probability"), font = list(size = 12)) ```
--- Cumulative normal probability distribution: .pull-left[ ```r dat_norm <- tibble( x = seq(-4, 4, length = 1001), f = dnorm(x = x, mean = 0, sd = 1), F = pnorm(q = x, mean = 0, sd = 1)) ``` ```r p4 <- ggplot(dat_norm) + geom_line(aes(x, F), size = 1, col = "darkblue") ggplotly(p4) %>% layout(title = "Cumulative Normal Plot", xaxis = list(title = "X"), yaxis = list(title = "Cumulative Normal Probability"), width = 500, height =420, font = list(size = 12)) ``` ] .pull-right[
] --- class: center, middle, inverse # patchwork --- <br><br><br> **patchwork** is a package which allows users to combine multiple packages in one single plot. Key features: - Interactive - Open source - Can work with other interactive R packages such a plotly - Supports html format - Very simple syntax <br><br><br> --- The basic plots: ```r p1<-ggplot(penguins)+ geom_histogram(aes(x=body_mass_g, y=after_stat(density)), col="red", fill="blue") p2<-ggplot(penguins)+ geom_boxplot(aes(x=body_mass_g, y=species, fill=species)) p3<-ggplot(penguins)+ geom_point(aes(x = bill_length_mm, y = flipper_length_mm, col=species))+ geom_smooth(aes(x = bill_length_mm, y = flipper_length_mm), col="red", method = "lm") ``` --- ```r p1 ``` <img src="44_files/figure-html/plot-X1-out-1.png" width="100%" /> --- ```r p2 ``` <img src="44_files/figure-html/plot-X2-out-1.png" width="100%" /> --- ```r p3 ``` <img src="44_files/figure-html/plot-X3-out-1.png" width="100%" /> --- ##Combined plots: ```r p1+p2/p3 ``` <img src="44_files/figure-html/unnamed-chunk-5-1.png" width="100%" /> --- ```r (p1+p2)/p3 ``` <img src="44_files/figure-html/unnamed-chunk-6-1.png" width="100%" /> --- ```r p1/(p2+p3) ``` <img src="44_files/figure-html/unnamed-chunk-7-1.png" width="100%" /> --- class: center, middle, inverse # hrbrthemes --- **hrbrthemes** is an extension of the **ggplot2** package for creating more visually appealing and thematic plots. .pull-left[ ```r ggplot(penguins, aes(x = bill_length_mm, y = body_mass_g ))+ geom_point()+ theme_modern_rc( base_family = "serif", base_size = 14)+ theme(panel.grid.major = element_line( size=1.1, color = "purple"))+ theme(panel.grid.minor = element_line( size=1.1, color ="blue") ) ``` ] .pull-right[ <!-- --> ] --- class: center, middle, inverse # dygraphs --- <br><br><br> **Dygraphs** is a JavaScript charting library, which offers interactive data visualize of time series data in R. - Time series analysis - Zoomable and interactive - Cross browser compatible - Real time updates - Supports csv and json files <br><br><br> --- Using range selector: ```r dygraph(data = nhtemp, main = "Temperatures New Haven", width = 700, height = 420) %>% dyRangeSelector() ``` --
--- Multiple time series graph: ```r lungDeaths = cbind(mdeaths, fdeaths) dygraph(data = lungDeaths, width = 700, height = 380) %>% dySeries(name = "mdeaths", label = "Male",color = "purple") %>% dySeries(name = "fdeaths", label = "Female",color = "orange")%>% dyRangeSelector() ```
--- class: center, middle, inverse # ggiraph --- <br><br><br> **ggiraph** is an R package that extends the functionality of the popular **ggplot2** package by adding interactive features to your static ggplots <br><br><br> Key features of **ggiraph** includes: - Interactive elements such as tooltips, clickable elements and more - HTML output <br><br><br> --- Linking plots using **ggiraph**: ```r asia <- gapminder %>% filter(continent == "Asia") %>% select(year, country, lifeExp) p1 <- asia %>% filter(year == 1982) %>% ggplot(aes(y = reorder(country, lifeExp), x=lifeExp, tooltip = lifeExp, data_id = country)) + geom_bar_interactive(stat="identity", fill="steelblue") + labs(y="", x="1982") + theme_minimal() p2 <-asia %>% filter(year == 2007) %>% ggplot(aes(y = reorder(country, lifeExp), x=lifeExp, tooltip = lifeExp, data_id = country)) + geom_bar_interactive(stat="identity", fill="steelblue") + labs(y="", x="2007") + theme_minimal() p3 <- (p1 | p2) + plot_annotation(title = "Life Expectancy in Asia") girafe(code = print (p3), height_svg = 5) ``` ---
--- class: center, middle, inverse # Thank You :")