plotly/plotly.R

ggplotly() doesn't support multiple colors in axis ticks

nicocriscuolo opened this issue · 2 comments

Hello and thanks for the great job you did by making this really useful package. I just want to report an issue that appears when I need to colour the axis text of a plot with pre - defined colours. Below you can see the R code to build a really simple shiny app. If I plot my graph with the standard ggplot() function the names of the x axis result colored, but if I use the function ggplotly() to make my plot interactive, the text of the x axis remains black (the functions recognizes the arguments "angle" and "hjust" but not "col").

Thanks in advance for the availability.

Nico

library(ggplot2)
library(plotly)
library(shiny)

### UI
ui <- fluidPage(
  fluidRow(
    column(width = 8,
           plotlyOutput(outputId = "plot", 
                        height = "600px"))))

### SERVER
server <- function(input, output) {
  
output$plot <- renderPlotly({
  
  mpg_s <- mpg[c(1, 19, 38), ] # subset to pick just 3 rows
  
  col <- c("red", "green", "blue")
  
  p <- ggplot(data = mpg_s,
              aes(x = manufacturer, y = hwy)) +
         geom_point() +
         theme(axis.text.x = element_text(size = 20,
                                         angle = 90,
                                         hjust = 1,
                                         colour = col))
  
  ggplotly(p)

  })
}

### LUNCH APP
shinyApp(ui = ui, server = server)

More minimal example:

library(plotly)

mpg_s <- mpg[c(1, 19, 38), ] # subset to pick just 3 rows

col <- c("red", "green", "blue")

ggplot(data = mpg_s,
            aes(x = manufacturer, y = hwy)) +
    geom_point() +
    theme(axis.text.x = element_text(size = 20,
                                     angle = 90,
                                     hjust = 1,
                                     colour = col))

screen shot 2018-11-01 at 12 54 02 pm

It looks like plotly.js doesn't support this sort of thing (all the ticktext on a given axis must be the same color). I've made a feature request in plotly/plotly.js#3204

I would also like to color axis ticks in my ggplotly based on a vector of colors passed to axis.ticks.x for the ggplot, using:

theme(axis.ticks.x = element_line(color = col))