My goal is to make a bar plot with images as axis labels, like this (screen 1). I also want plotly's chart annotations, but adding plotly to the program causes the axis labels to be interpreted as raw text instead of images (screenshot 2).
Code:
library("shiny")
library('bslib')
library("ggplot2")
library('ggtext')
library('plotly')
# Example data. The project's www/ directory contains apple.png, mangosteen.png, etc.
df <- data.frame('fruit' = c('Apple', 'Mangosteen', 'Pear', 'Banana'),
'quantity' = c(5, 4, 1, 3))
ui <- page_fluid(plotlyOutput('groceries_plot'))
server <- function(input, output) {
# Add image paths that can be rendered with ggtext's markdown feature.
df <- df %>% mutate(html_img = paste0("<img src='www/",tolower(fruit),".png'",
" height = '95'><br>", fruit))
output$groceries_plot <- renderPlotly({
ggplot(df, aes(x = html_img, y = quantity, fill = fruit)) +
geom_col() +
theme_light() +
# Should read html_img as markdown - instead gives raw text.
theme(axis.text.x = ggtext::element_markdown())
})
}
runApp(list(ui = ui, server = server))
It looks like plotly is interacting with ggtext's markdown(). If plotlyOutput is changed to plotOutput and renderPlotly is changed to renderPlot, I get screenshot 1, but the version above gives screenshot 2.

plot_grid()while specifying the relative heights and aligning them.