> For the complete documentation index, see [llms.txt](https://laboratory-of-lipid-metabolism-a.gitbook.io/omics-data-visualization-in-r-and-python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://laboratory-of-lipid-metabolism-a.gitbook.io/omics-data-visualization-in-r-and-python/metabolites-and-lipids-descriptive-statistical-analysis-in-r/creating-interactive-plots-with-ggplotly.md).

# Creating interactive plots with ggplotly

The plotly library contains the **ggplotly()** function, which will allow you to turn some of your ggplot2 graphics into an interactive plotly object. Such interactive figures can be used for exploratory data analysis or as a part of a presentation, e.g. for your lab meetings. Here is how the ggplotly() function works:

```r
# Changing ggplot2 box plots with dots into interactive plot:
# Calling libraries:
library(plotly)
library(tidyverse)

# Consulting the documentation:
?ggplotly()

# Create 'data.long' tibble:
data.long <- data %>%
  select(`Label`,
         `SM 39:1;O2`,
         `SM 40:1;O2`,
         `SM 41:1;O2`,
         `SM 42:1;O2`) %>%
  pivot_longer(cols = `SM 39:1;O2`:`SM 42:1;O2`,
               names_to = 'Lipids',
               values_to = 'Concentrations')

# Creating a plot:
box.plots <- 
  ggplot(data.long, aes(x = `Label`, 
                      y = `Concentrations`,
                      fill = `Label`)) +
  geom_boxplot(outlier.shape = NA) +
  geom_point(position = position_jitter(width = 0.05), shape = 21) +
  scale_fill_manual(values = c('royalblue', 'orange', 'red2')) +
  facet_grid(. ~ Lipids) + 
  theme_classic() +
  theme(strip.placement = "outside",
        strip.background = element_blank(),
        panel.border = element_blank(),
        panel.spacing.x = unit(0, 'cm')) 

# Changing the plot into an interactive version:
ggplotly(box.plots)
```

The output:

<figure><img src="/files/mcYxkPZLlipZYgDnNGQe" alt=""><figcaption><p>Interactive box plots are generated using ggplotly() function from the plotly library.</p></figcaption></figure>

We can even create entire interactive correlation heat maps:

```r
# Interactive correlation heat maps.
# Calling library:
library(reshape2)
library(ggsci)

# Computing correlation matrix:
corr.matrix <- 
  data %>%
  filter(Label == "N") %>%
  select(starts_with("SM"),
         -`Sample Name`,
         -Label) %>%
  cor()
  
# Melting a wide correlation matrix into a long correlation matrix:
long.corr.matrix <- melt(corr.matrix)

# Selecting colors for the continuous color scaling:
colors <- c("#002060", "#0d78ca", "#00e8f0", "white", "#FF4D4D", "red", "#600000")

# Creating a ggplot2 correlation heat map:
corr.heat.map <-
  ggplot(long.corr.matrix, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile() +
  scale_fill_gradientn(colours = colors, limits = c(-1,1)) +
  theme(axis.text.x = element_text(angle = 90, 
                                   hjust = 1, 
                                   vjust = 0.5))
                                   
# Creating the interactive plotly heat map:
ggplotly(corr.heat.map)
```

The output:

<figure><img src="/files/xyADN98N1CcaFOidyZmQ" alt=""><figcaption><p>The ggplot2 correlation heat map turned into a plotly interactive plot using ggplotly().</p></figcaption></figure>

We encourage you to explore more of the possibilities offered by the ggplotly() function.
