I recommend ggplotly() for quick interactive plots. And
{highcharter} for more polished dashboard plots.
# Load packages
if(!require(pacman)) install.packages("pacman")
pacman::p_load(tidyverse, plotly, highcharter)
p <- ggplot(iris,
aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
facet_wrap(~ Species) +
labs(title = "A title for my plot",
color = "A custom label",
caption = "A caption for my plot")
p Use the ggplotly() function. Plotly is hard to make
beautiful though, in my experience.
ggplotly(p)First a basic plot.
highchart() %>%
hc_add_series(data = iris, type = "scatter",
hcaes(x = Sepal.Length,
y = Sepal.Width,
color = Species)) %>%
hc_xAxis(title = list(text = "Sepal.Length")) %>%
hc_yAxis(title = list(text = "Sepal.Width")) %>%
hc_title(text = "A title for my beautiful plot") %>%
hc_subtitle(text = "A subtitle for my wonderful plot") %>%
hc_add_theme(hc_theme_flat()) %>%
hc_credits(enabled = TRUE, text = "custom caption")To get facets you need to do a bit of work.
# Create a list of plots, one for each species
list_of_plots <- map(unique(iris$Species), function(x) {
# Filter the data to only include the current species
filtered <- iris %>% filter(Species == x)
spec_color <- c("setosa" = "#2074bd",
"virginica" = "#cc5a74",
"versicolor" = "#51a898")[[x]]
# Create a chart with the filtered data
highchart() %>%
hc_add_series(data = filtered, type = "scatter",
hcaes(x = Sepal.Length, y = Sepal.Width),
color = spec_color) %>%
hc_xAxis(title = list(text = "Sepal.Length")) %>%
hc_yAxis(title = list(text = "Sepal.Width")) %>%
hc_title(text = x) %>%
hc_add_theme(hc_theme_flat()) %>%
hc_legend(enabled = FALSE) %>%
hc_credits(enabled = TRUE, text = "custom caption")
})
# Use the hw_grid() function to combine the charts
hw_grid(list_of_plots, rowheight = 300) %>%
htmltools::browsable()Here is a highcharter showcase: https://jkunst.com/highcharter/articles/showcase.html
Here is a highcharter tutorial: https://www.tmbish.me/lab/highcharter-cookbook/