How to convert a ggplot2 chart into an interactive chart

I need to convert this chart to interactive.

ggplot(data = mtcars, aes(x = mpg, y = cyl)) +
  geom_point() +
  theme_bw() +
  labs(title = "MPG por Cilindros", x = "MPG", y = "Cilindros") +
  scale_x_continuous(breaks = seq(15, 40, by = 5), breaks_label = function(x) paste0(x, " mpg")) + 
  scale_y_continuous(limits = c(3, 8), minor_grid_line = element_line(color = "gray88")) + 
  theme(plot.title = element_text(hjust = 0.5), 
        axis.title = element_text(size = 12, face = "bold"), 
        legend.position = "top.right",
        legend.title = element_text(face = "italic")) 

I don't know what kind of interactivity you're looking for, but if you just wan to be able to hover over data points and get a pop-up to show its values, plotly might do it for you:

library(ggplot2)
library(plotly)
p <-  ggplot(data = mtcars, aes(x = mpg, y = cyl)) +
  geom_point() +
  theme_bw() +
  labs(title = "MPG por Cilindros", x = "MPG", y = "Cilindros") +
  scale_x_continuous(breaks = seq(15, 40, by = 5), label = function(x) paste0(x, " mpg")) + 
  scale_y_continuous(limits = c(3, 8)) + 
  theme(plot.title = element_text(hjust = 0.5), 
        axis.title = element_text(size = 12, face = "bold"), 
        legend.position = "top.right",
        legend.title = element_text(face = "italic"))

ggplotly(p)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.