Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change tick mark labels to specific strings in plotly

Tags:

r

ggplot2

plotly

I have plotly graphics where I'd like to change axis tick mark labels to specific strings. Consider the following example:

library(plotly)
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)

enter image description here

Say I want to supress all numeric xaxis tick mark labels and instead only plot the string "min" at x = 4.5 and "max" at x = 8.

How can I achieve this in plotly?


Sidenote: I know this is possible for base R plots, e.g. here and in ggplot2 by setting scale_x_continuous(breaks = c(4.5, 8), labels= c("4.5" = "min", "8" = "max")).

Is there also a way to achieve this in plotly? ..Unfortunately the plotly docs don't seem to offer a solution..

like image 659
symbolrush Avatar asked Sep 12 '18 06:09

symbolrush


1 Answers

Thanks to @eipi10 and @Jon Spring I could figure it out:

library(plotly)
plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length) %>% 
  layout(xaxis = list(tickvals = c(4.5, 8), ticktext = c("min", "max")))

This is exactly what I was searching for and produces:

enter image description here

like image 167
symbolrush Avatar answered Sep 29 '22 00:09

symbolrush