class: center, middle, inverse, title-slide # Interactive Graphics ### Elizabeth Borgognoni Souto ### 2017-11-06 --- ## Pre-requisites To run this tutorial, you'll need: - Packages: dplyr, tydir, broom, ggplot2, plotly, highcharter ```r packages = c('dplyr', 'tydir', 'broom', 'ggplot2', 'plotly', 'highcharter') install.packages(packages) ``` --- ## Packages to Interactive Graphics - The `plotly` is R package for creating interactive web-based graphs via plotly's JavaScript graphing library, plotly.js. The `ploty` R library contains a function ggplotly which will convert ggplot2 figures into graphs drawn with plotly.js which can be saved to your online plotly account or rendered locally. - The `highcharter` package enables the creation of Highcharts type plots within R. `highcharter` provides a rich R interface to the popular Highcharts J. The functions of `highcharter` use standar evaluation. Recommended to make the final graph instead of using the package to visually explore the data. ```r library("dplyr") # for data manipulation library("tidyr") # for data manipulation library("broom") # for data statistical manipulation library("ggplot2") # for plotting power curves library("plotly") # for interactive power curves library("highcharter") # for interactive power curves ``` --- ## Plotly Benefits: - Using with shiny - 100% Free and Open-Source, Forever (MIT license) - Statistical and engineering charts - Plotly’s ggplot2 converter turns ggplot2 plots into interactive, web-based plots. You can control the tooltip. - Plotly objects are data frames with a class of plotly and an environment that tracks the mapping from data to visual properties. - Plotly also supports interactive maps - Plotly offline using the Plotly R client, Shiny and RStudio Negative points - Complicated to work with a large volume of data - When use ggplot2 with plotly, ggplot2 doesn't know how to deal with data of class numeric --- See more in : [https://plot.ly/](About plotly) [https://plot.ly/ggplot2/](About plotly with ggplot2) --- ## Using Plotly with ggplot2 ```r set.seed(1234) dat <- data.frame( cond = factor(rep(c("trace 0", "trace 1"), each = 50)), rating = c(rnorm(50), rnorm(50, mean = 1))) mytheme <- theme_dark() + theme(text = element_text(colour="blue"), axis.title = element_text(size = rel(1.25))) #creating my own theme p<-ggplot(dat, aes(x = cond, y = rating, fill = cond)) + geom_boxplot() + ggtitle("Box Plot") + labs(fill="Condition") + scale_fill_brewer(palette="Dark2") + geom_text(label = 0) + annotate("text", label = "outlier", x= dat$cond[20], y = 2.6, size = 3, colour = "blue") + ##add name to the outlier mytheme ggplotly(p) ``` ---
--- Box plot with Jittered Points ```r set.seed(1234) dat<- data.frame( cond = (rep(c("trace 0", "trace 1"), each = 50)), rating = c(rnorm(50), rnorm(50, mean = 1)) ) p<-ggplot(dat,aes(x = cond, y = rating, fill = cond)) + geom_boxplot() + ggtitle("Box plot") + labs(fill="Condition") + scale_fill_brewer(palette="Accent") + geom_point(position = position_jitter(width = 0.2))+ theme_bw() + theme(panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank()) ggplotly(p) ``` ---
--- ## Same example without ggplot2 ```r set.seed(1234) a<-rnorm(50) a2 <- rnorm(50, 1) plot_ly(y = a, type = 'box') %>% add_trace(y = a2) %>% layout(title = 'Box Plot', xaxis = list(title = "cond", showgrid = F), yaxis = list(title = "rating"), annotations = list( x = -0.01, y = boxplot.stats(a)$out, text = "Outlier", showarrow = FALSE, xanchor = "right" )) ``` ---
--- ##Highcharter Benefits: - Using with shiny - Plots are built like ggplot2 by layering, although they use the pipe operator (%>%) instead of + - It is possible to configure your plots with pre-implemented themes like Economist, Financial Times, Google, and FiveThirtyEight among others - There are some Plugins: Motion, drag points, fontawesome, url-pattern, annotations. - Has more tools than plotly - The posibility to create or modify themes and customize in every way your chart: beatiful tooltips, titles, credits, legends, add plotlines or plotbands. Negative points: - Because has more tools is more difficult, sometimes you need to use another package to modificate your data - The grapich is interactive but doesn't have the bar with tools like plotly --- See more in: [https://www.rstudio.com/rviews/2016/10/19/creating-interactive-plots-with-r-and-highcharts/](Examples Highcharts) [http://jkunst.com/highcharter/](About Highcharts) [https://github.com/jbkunst/highcharter/blob/master/vignettes/charting-data-frames.Rmd](News Highcharter) --- ##Using Highcharter Start with empty chart and add components ```r set.seed(1234) dat <- data.frame( cond = (rep(c("trace 0", "trace 1"), each = 50)), rating = c(rnorm(50), rnorm(50, mean = 1)) ) hcboxplot(x = dat$rating, var = dat$cond, name = "cond", showInLegend = TRUE) %>% hc_chart(type = "column") %>% hc_title(text = "Box Plot") ``` ---
--- ##It's possible modificate themes, colors and modificate the legend ```r set.seed(1234) dat <- data.frame( cond = (rep(c("trace 0", "trace 1"), each = 50)), rating = c(rnorm(50), rnorm(50, mean = 1)) ) highchart() %>% hc_title(text = "Box Plot") %>% hcboxplot(x = dat$rating, var = dat$cond, name = "cond", showInLegend = TRUE) %>% hc_chart(type = "column") %>% hc_legend(align = "right", verticalAlign = "top", layout = "vertical", x = 0, y = 100, enable = TRUE ) %>% hc_add_theme(hc_theme_sandsignika()) ``` ---
--- ##Plotly with ggplot, geom_smooth Linear Regression ```r set.seed(1234) dat <- data.frame(cond = rep(c("A", "B"), each=50), xvar = 1:50 + rnorm(50,sd=3), yvar = 1:50 + rnorm(50,sd=3)) p<-ggplot(dat, aes(x=xvar, y=yvar)) + geom_point(shape=1, colour = "red") + geom_smooth(method=lm) + ggtitle("Linear Regression") ggplotly(p) ``` ---
--- ##Linear regression using highcharts ```r set.seed(1234) dat <- data.frame(cond = rep(c("A", "B"), each=50), xvar = 1:50 + rnorm(50,sd=3), yvar = 1:50 + rnorm(50,sd=3)) highchart() %>% hc_add_series(dat, "point", hcaes(xvar, yvar, group = cond), regression = TRUE) %>% hc_add_series(lm(yvar ~ xvar, data = dat), name = "Regression") %>% hc_add_theme(hc_theme_google()) ``` ---
--- ##Last Example ##three clusters, within which points are distributed according to a multivariate gaussian Plotly ```r library("dplyr") set.seed(1234) centers <- data.frame(cluster=factor(1:3), size=c(100, 150, 50), x1=c(5, 0, -3), x2=c(-1, 1, -2)) points <- centers %>% group_by(cluster) %>% do(data.frame(x1=rnorm(.$size[1], .$x1[1]), x2=rnorm(.$size[1], .$x2[1]))) p<-ggplot(points, aes(x1, x2, color=cluster)) + geom_point() + ggtitle("Multivariate gaussian") ggplotly(p) ``` ---
--- ##Last Example Highcharter ```r library("dplyr") set.seed(1234) centers <- data.frame(cluster=factor(1:3), size=c(100, 150, 50), x1=c(5, 0, -3), x2=c(-1, 1, -2)) points <- centers %>% group_by(cluster) %>% do(data.frame(x1=rnorm(.$size[1], .$x1[1]), x2=rnorm(.$size[1], .$x2[1]))) highchart() %>% hc_add_theme(hc_theme_smpl()) %>% hc_title(text = "Multivariate gaussian") %>% hc_add_series(points, type = "scatter", hcaes(x = x1, y = x2, group = cluster), showInLegend = TRUE) ``` ---
--- Therefore, we note that the two packages are good each with its benefits and negatives points. It concludes that the use of each of them depends on the type of analysis that is done and what you want to demonstrate in your chart. Highcharter has many interesting themes that act on the beauty of the chart, its graphics are geared more to the final graph and not to analysis. Thus it is recommended to analyze your data first with the ggplot2 package, and then transform into interactive graph. In this matter plotly wins because it is easier to transform the graph of ggplot2 to ploty. The problem of plotly is that sometimes with the amount of graph data volume it does not support interactivity. However, plotly has more graphics focused on statistics than highcharter. The other packages presented are ways to improve your data and arrange them to take less work when plotting the graph. Especially when use highcharter to make statistical charts. The latest highchart update was very promising, focused on the style of the ggplot2 package.