class: center, middle, inverse, title-slide .title[ # Plotly Party ] .author[ ### MACS 40700
University of Chicago ] --- class: inverse # Data visualization: Agenda for today * Basics of plotly * Graphic applications * Converting a ggplot graphic * Shiny integration --- # Plotly plotly is an R package for creating interactive web-based graphs via the open source JavaScript graphing library plotly.js. Graphs created with the plotly R package are rendered locally through the htmlwidgets framework. Plotly graphs are great as they have a nice interactive/mouseover component (aka tooltips). --- # Plotly: getting started ``` r install.packages("plotly") ``` ``` r library(plotly) fig <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box", height = 250) fig ```
--- # Plotly: options * Bar plots * Scatter plots * Line plots * Dot plots * Barbel plots * Boxplots --- # Bar ``` r fig <- plot_ly( x = c("giraffes", "orangutans", "monkeys"), y = c(20, 14, 23), name = "SF Zoo", type = "bar", height = 150 ) fig ```
--- # Scatter plots ``` r plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, height = 400, type = "scatter") ```
--- ## Converting -- pretty easy to convert from prior plots .panelset[ .pull-left[
] .pull-right[ ``` r p <- ggplot(scorecard, aes(x = type, y = avgfacsal)) + geom_point(alpha = 0.5) + stat_summary(geom = "point", fun = "median", color = "red", size = 5, pch = 4, stroke = 2) ggplotly(p) ``` from https://macs40700.netlify.app/slides/05-stats-scales-guides/#11 ] ] --- ## Line plots ``` r trace_0 <- rnorm(100, mean = 5) trace_1 <- rnorm(100, mean = 0) trace_2 <- rnorm(100, mean = -5) x <- c(1:100) data <- data.frame(x, trace_0, trace_1, trace_2) fig <- plot_ly(data, x = ~x, height = 200) fig <- fig %>% add_trace(y = ~trace_0, name = 'trace 0',mode = 'lines') fig <- fig %>% add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers') fig <- fig %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers') fig ```
--- ## Dot plots ``` r p <- ggplot(mtcars, aes(x = mpg, fill = factor(cyl))) + geom_dotplot(stackgroups = TRUE, binwidth = 1, binpositions = "all") ggplotly(p, height = 325) ```
--- ## Barbel/Dumbell plots .panelset[ .panel[.panel-name[Dumbell plots] ``` r gapminder <- read_csv("https://raw.githubusercontent.com/datavizpyr/data/master/gapminder-FiveYearData.csv") df <- gapminder %>% filter(year %in% c(1952,2007)) %>% filter(continent=="Asia") df <- df %>% mutate(paired = rep(1:(n()/2),each=2), year=factor(year)) p <- df %>% group_by(paired) %>% ggplot(aes(x= lifeExp, y= reorder(country,lifeExp))) + geom_line(aes(group = paired),color="grey")+ geom_point(aes(color=year), size=2) + scale_color_viridis_d(name = "Year", option = "turbo") + labs(y="country") + theme(text = element_text(size = 10)) ggplotly(p, height = 450) ```
] .panel[.panel-name[Plot]
] ] --- ## Colorscales Just like in ggplot, you can color by variables. ``` r plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species, height = 300) ```
--- ## Colorscales ``` r library(palmerpenguins) plot_ly(data = penguins, x = ~bill_length_mm , y = ~body_mass_g, color = ~sex, height = 300) ```
--- ## ColorBrewer Palette Names ``` r plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species, colors = "Set1", height = 300) ```
--- ## Custom Color Scales ``` r pal <- c("red", "blue", "orange") plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species, colors = pal, height = 300) ```
--- ## Mapping Data to Symbols ``` r plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, type = 'scatter', mode = 'markers', symbol = ~Species, symbols = c('circle','x','o'), alpha = 0.5, color = I('black'), marker = list(size = 10), height = 300) ```
--- ## Data labels on hover ``` r d <- diamonds[sample(nrow(diamonds), 1000), ] plot_ly( d, x = ~carat, y = ~price, # Hover text: text = ~paste("Price: ", price, '$<br>Cut:', cut), color = ~carat, size = ~carat, height = 300) ```
--- # Boxplots ``` r set.seed(1234) dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), rating = c(rnorm(200),rnorm(200, mean=.8))) p <- ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot() ggplotly(p, height = 350) ```
--- ### Boxplot conversion from: https://macs40700.netlify.app/slides/05-stats-scales-guides/?panelset_001=geom#14 ``` r data("PlantGrowth") p<-ggplot(PlantGrowth, aes(x = group, y = weight)) + geom_boxplot() + stat_summary(geom = "point", fun = "mean", color = "darkgreen", size = 5, pch = 4, stroke = 2) ggplotly(p) ```
--- ## Data labels on hover ``` r d <- diamonds[sample(nrow(diamonds), 1000), ] plot_ly( dat, x = ~cond, y = ~rating, # Hover text: text = ~paste("Price: ", cond), color = ~cond, type = "box", height = 200) ```
--- ## Additional plot types * Just about any other plot type can be done -- try it on your own * Animated plots * Area plots * Bubble plots * Contour plots * Heatmaps * Spatial maps * Etc. --- # Application: Try converting a ggplot graph .panelset[ .panel[.panel-name[ggplot version] ``` r ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species), ) + geom_point(size = 2) ``` <img src="index_files/figure-html/unnamed-chunk-17-1.png" width="500px" style="display: block; margin: auto;" /> ] .panel[.panel-name[plotly version] ``` r p <- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) + geom_point(size = 2) ggplotly(p, height = 220) ```
] ] --- # Recap * Plotly is another way to plot things as it offers interactivity. It can be used in R or Python and the plots can be quite nice if you're making slides or anything that's going to be online in some way. * Sometimes you can incorporate ggplot elements and then render in ggplot * Just about any plot you make in ggplot you can make in plotly * Boxplots * Bar plots * Scatter plots * Line plots * ... etc. ... --- ## Other options: ggiraph <iframe src="https://davidgohel.github.io/ggiraph/" width="80%" height="400px" data-external="1"></iframe> --- ## SHINY PARTY (REDUX) We are going to bring together all of our skills from the course (!!): * Shiny app * Interactive (plotly) plot * Strong design principles --- # How do you feel about BAKING??? * `install.packages(bakeoff)` --- # Cool things: closeread + battle with Quarto <iframe src="https://closeread.dev/gallery/" width="80%" height="400px" data-external="1"></iframe> --- ## Quarto: an alternative * Different way to render/work with R (similar to RMD files) * Can work with different packages (logic can be a little python-y w/r/t virual environments) --- ## Closeread * Allows for cool scrolling * Makes HTML files * Can combine with other class skills * Tempting for final project * PATIENCE NEEDED --- ## Closeread simple example * download our repo * navigate to the folder in the terminal * run `quarto add qmd-lab/closeread` * be nice to coding gods https://closeread.dev/gallery/ --- ### Closeread errors * using cloud storage for device * wrong package libraries * added closeread to wrong directory file (one level up)