This document discusses using ggplotly() to convert ggplot2 graphs to interactive plotly graphs. It provides examples converting a hexbin plot of diamond carat versus price, frequency polygons of diamond price by clarity, and frequency polygons faceted by cut. ggplotly() allows leveraging ggplot2's interface while adding interactivity for exploration. It generates one trace per group to populate tooltips and legends.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
70 views
Use Plotly
This document discusses using ggplotly() to convert ggplot2 graphs to interactive plotly graphs. It provides examples converting a hexbin plot of diamond carat versus price, frequency polygons of diamond price by clarity, and frequency polygons faceted by cut. ggplotly() allows leveraging ggplot2's interface while adding interactivity for exploration. It generates one trace per group to populate tooltips and legends.
unique(purrr::map_chr(b$x$data, "type")) #> [1] "histogram" Here we’ve learned that plotly creates 8 histogram traces to generate the dodged bar chart: one trace for each level of clarity.6 Why one trace per category? As illustrated in Figure 2.7, there are two main reasons: to populate a tooltip and legend entry for each level of clarity level. FIGURE 2.7: Leveraging two interactive features that require one trace per level of clarity: (1) Using ‘Compare data on hover’ mode to get counts for every level of clarity for a given level of cut and (2) Using the ability to hide/show clarity levels via their legend entries. For the interactive, see https://plotly- r.com/interactives/intro-show-hide.html If we investigated further, we’d notice that color and colors are not officially part of the plotly.js figure definition – the plotly_build() function has effectively transformed that information into a sensible plotly.js figure definition (e.g., marker.color contains the actual bar color codes). In fact, the color argument in plot_ly() is just one example of an abstraction the R package has built on top of plotly.js to make it easier to map data values to visual attributes, and many of these are covered in Chapter 3. 2.3 Intro to ggplotly() The ggplotly() function from the plotly package has the ability to translate ggplot2 to plotly. This functionality can be really helpful for quickly adding interactivity to your existing ggplot2 workflow.7 Moreover, even if you know plot_ly() and plotly.js well, ggplotly() can still be desirable for creating visualizations that aren’t necessarily straight-forward to achieve without it. To demonstrate, let’s explore the relationship between price and other variables from the well-known diamonds dataset. Hexagonal binning (i.e., geom_hex()) is useful way to visualize a 2D density 8, like the relationship between price and carat as shown in Figure 2.8. From Figure 2.8, we can see there is a strong positive linear relationship between the log of carat and price. It also shows that for many, the carat is only rounded to a particular number (indicated by the light blue bands) and no diamonds are priced around $1500. Making this plot interactive makes it easier to decode the hexagonal colors into the counts that they represent. p <- ggplot(diamonds, aes(x = log(carat), y = log(price))) + geom_hex(bins = 100) ggplotly(p) FIGURE 2.8: A hexbin plot of diamond carat versus price.
I often use ggplotly() over plot_ly() to leverage ggplot2’s consistent and
expressive interface for exploring statistical summaries across groups. For example, by including a discrete color variable (e.g., cut) with geom_freqpoly(), you get a frequency polygon for each level of that variable. This ability to quickly generate visual encodings of statistical summaries across an arbitrary number of groups works for basically any geom (e.g. geom_boxplot(), geom_histogram(), geom_density(), etc) and is a key feature of ggplot2. p <- ggplot(diamonds, aes(x = log(price), color = clarity)) + geom_freqpoly() ggplotly(p) FIGURE 2.9: Frequency polygons of diamond price by diamond clarity. This visualization indicates there may be significant main effects.
Now, to see how price varies with both cut and clarity, we could repeat this same
visualization for each level of cut. This is where ggplot2’s facet_wrap() comes in handy. Moreover, to facilitate comparisons, we can have geom_freqpoly() display relative rather than absolute frequencies. By making this plot interactive, we can more easily compare particular levels of clarity (as shown in Figure 2.10) by leveraging the legend filtering capabilities. p <- ggplot(diamonds, aes(x = log(price), color = clarity)) + geom_freqpoly(stat = "density") + facet_wrap(~cut) ggplotly(p)
Instant Download (Ebook) Interactive Web-Based Data Visualization with R, Plotly, and Shiny by Carson Sievert ISBN 9781138331457, 1138331457 PDF All Chapters
Instant Download (Ebook) Interactive Web-Based Data Visualization with R, Plotly, and Shiny by Carson Sievert ISBN 9781138331457, 1138331457 PDF All Chapters