0% found this document useful (0 votes)
38 views

# Fake Some Missing Data: Library Ifelse Ggplot Aes Log Geom - Miss - Point Stat - Summary Facet - Wrap Towebgl Ggplotly

The document discusses using the ggplotly() function to convert ggplot2 graphs into interactive web-based visualizations. It summarizes that ggplotly() allows adding interactivity like hover, zoom, and filter capabilities to exploratory data visualizations created with ggplot2. While ggplotly() conversions usually work well, sometimes modifications are needed to customize aspects like tooltips. Links to other visualization and interactive graphic techniques are provided.

Uploaded by

Seshendra Vemuri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

# Fake Some Missing Data: Library Ifelse Ggplot Aes Log Geom - Miss - Point Stat - Summary Facet - Wrap Towebgl Ggplotly

The document discusses using the ggplotly() function to convert ggplot2 graphs into interactive web-based visualizations. It summarizes that ggplotly() allows adding interactivity like hover, zoom, and filter capabilities to exploratory data visualizations created with ggplot2. While ggplotly() conversions usually work well, sometimes modifications are needed to customize aspects like tooltips. Links to other visualization and interactive graphic techniques are provided.

Uploaded by

Seshendra Vemuri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

library(naniar)

# fake some missing data


diamonds$price_miss <- ifelse(diamonds$depth>60, diamonds$price, NA)
p <- ggplot(diamonds, aes(x = clarity, y = log(price_miss))) +
geom_miss_point(alpha = 0.1) +
stat_summary(fun.data = "mean_cl_boot", colour = "black") +
facet_wrap(~cut)
toWebGL(ggplotly(p))
FIGURE 2.14: Using the geom_miss_point() function from the naniar package to
visualize missing values in relation to non-missing values. Missing values are
shown in red.
In short, the ggplot2 ecosystem provides a world-class exploratory visualization
toolkit, and having the ability to quickly insert interactivity such as hover, zoom,
and filter via ggplotly() makes it even more powerful for exploratory analysis. In
this introduction to ggplotly(), we’ve only seen relatively simple techniques that
come for free out-of-the-box, but the true power of interactive graphics lies in
linking multiple views. In that part of the book, you can find lots of examples of
linking multiple ( ggplotly() & plot_ly()) graphs purely client-side as well as
with shiny.
It’s also worth mentioning that ggplotly() conversions are not always perfect
and ggplot2 doesn’t provide an API for interactive features, so sometimes it’s
desirable to modify the return values of ggplotly(). Chapter 33 talks generally
about modifying the data structure underlying ggplotly() (which, by the way, uses
the same a plotly.js figure definition as discussed in Section 2.2). Moreover,
Chapter 25.2 outlines various ways to customize the tooltip
that ggplotly() produces.

References
Bache, Stefan Milton, and Hadley Wickham. 2014. Magrittr: A Forward-Pipe
Operator for R. https://CRAN.R-project.org/package=magrittr.

Harrell Jr, Frank E, with contributions from Charles Dupont, and many others.
2019. Hmisc: Harrell Miscellaneous. https://CRAN.R-project.org/package=Hmisc.

Ooms, Jeroen. 2014. “The Jsonlite Package: A Practical and Consistent Mapping
Between Json Data and R Objects.” arXiv:1403.2805
[stat.CO]. http://arxiv.org/abs/1403.2805.

Pedersen, Thomas Lin. 2019. Ggforce: Accelerating ’Ggplot2’. https://CRAN.R-


project.org/package=ggforce.

Schloerke, Barret, Jason Crowley, Di Cook, Francois Briatte, Moritz Marbach,


Edwin Thoen, Amos Elberg, and Joseph Larmarange. 2016. GGally: Extension to
’Ggplot2’.

Tierney, Nicholas, Di Cook, Miles McBain, and Colin Fay. 2018. Naniar: Data
Structures, Summaries, and Visualisations for Missing
Data. https://github.com/njtierney/naniar.

Vaidyanathan, Ramnath, Yihui Xie, JJ Allaire, Joe Cheng, and Kenton Russell.
2016. Htmlwidgets: HTML Widgets for R. https://CRAN.R-
project.org/package=htmlwidgets.

Wickham, Hadley. 2009. Ggplot2: Elegant Graphics for Data Analysis. Springer-


Verlag New York. http://ggplot2.org.

Wickham, Hadley. 2010. “A Layered Grammar of Graphics.” Journal of


Computational and Graphical Statistics 19 (1): 3–28.
Wickham, Hadley, and Garrett Grolemund. 2018. R for Data Science. O’Reilly.

Wilkinson, Leland. 2005. The Grammar of Graphics (Statistics and Computing).


Secaucus, NJ, USA: Springer-Verlag New York, Inc.

2. If you aren’t already familiar with the grammar of graphics or ggplot2, we


recommend reading the Data Visualization chapter from the R for Data
Science book. https://r4ds.had.co.nz/data-visualisation.html↩︎

3. Technically speaking, these dplyr verbs are S3 generic functions


that plotly has a defined a custom method for. In nearly every case, that custom
method simply queries the data underlying the plotly object, applies
the dplyr function, then adds the transformed data back into the
resulting plotly object↩︎

4. A trace is similar in concept to a layer (as defined in Section 2.1, but it’s not
quite the same. In many cases, like the bottom panel of Figure 2.1, it makes sense
to implement a single layer as multiple traces. This is due to the design of plotly.js
and how traces are tied to legends and hover behavior.↩︎

5. The htmlwidgets package provides a foundation for other packages to


implement R bindings to JavaScript libraries so that those bindings work in various
contexts (e.g. the R console, RStudio, inside rmarkdown documents, shiny apps,
etc). For more info and examples, see the website http://www.htmlwidgets.org.↩︎

6. Although the x-axis is discrete, plotly.js still considers this a histogram


because it generates counts in the browser. Learn more about the difference
between histograms and bar charts in Chapter 5.↩︎

7. This section is not meant to teach you ggplot2, but rather to help point out
when and why it might be preferable to plot_ly(). If you’re new to ggplot2 and
would like to learn it, see 1.3.3.↩︎
8. See Section 7 for approaches using plot_ly()↩︎
9. As discussed in 34, ggplotly() can actually convert custom geoms as well,
but each one requires a custom hook, and many custom geoms are not yet
supported.↩︎

You might also like