Creating Interactive Plots with plotly
Instruction: Explain how to convert a ggplot2 plot to an interactive plotly graph, including interactive elements such as tooltips and zoom.
Context: This question tests the candidate's ability to enhance data visualization with interactivity using plotly, improving the data analysis experience.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
Build a ggplot object and pass it to plotly::ggplotly(p). The returned htmlwidget can provide hover details, zoom and pan when rendered in a compatible browser/viewer; a static image does not preserve those interactions.
For custom hover text, construct an intentional text field, map it with aes(text=...) in the ggplot, then call plotly::ggplotly(p, tooltip="text"). The tooltip selector belongs to ggplotly...