Home > Enterprise >  Plotly R : Format axis tick labels to log10
Plotly R : Format axis tick labels to log10

Time:07-24

I am creating line charts in plotly using plotly::ggplotly() with x and y-axis representing log10 values.

Is there a way to display x and y-axis tick labels as log10?

I have tried using tickformat = ".1r" but that doesn't seem to be working.

Thanks.

p <- ggplot(data, aes(x=x, y=y))  
     scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                   labels = trans_format("log10", math_format(10^.x)))  
     scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                   labels = trans_format("log10", math_format(10^.x)))

p <- plotly::ggplotly(p)

p <- p %>% layout(
           yaxis = list( 
                 tickformat=".1r"
                 ),
           xaxis = list(
                 tickformat=".1r"
                )
         )
p

CodePudding user response:

When you want to use something like superscripts in ggplot, you use commands like expression. When the data was sent through ggplotly, so was `expression.

If you wanted the axes labeled like the ggplot graph, you can remove the command expression and replace the ^ with <sup></sup> from the ggplotly object. However, for the y axes, the values aren't rounded, so I've shown you the literal result, the object valy. Then I rounded them in valy2. That way, whatever you were looking for might already be here.

I used p1, not p for the Plotly object.

Later in this answer, I'm going to show you what .1r will give you, which is unlikely what you were looking for.

library(tidyverse)
library(plotly)
library(scales)

set.seed(23)
df1 <- data.frame(x = rnorm(100, 1000000, 39999),
                  y = rnorm(100, 99999999999, 111111))
p <- ggplot(df1, aes(x = x, y = y))  
    geom_point()  
    scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                  labels = trans_format("log10", math_format(10^.x)))  
    scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                  labels = trans_format("log10", math_format(10^.x)))
p1 <- plotly::ggplotly(p)

valx <- p1$x$layout$xaxis$ticktext %>% 
  toString() %>% str_split(", ") %>%  # <---- that's a comma and whitespace
  unlist() %>% str_replace("\\^(.*)$", "<sup>\\1</sup>")    
valy <- p1$x$layout$yaxis$ticktext %>% 
  toString() %>% str_split(", ") %>%  # <---- that's a comma and whitespace
  unlist() %>% str_replace("\\^(.*)$", "<sup>\\1</sup>")
valy
# [1] "10<sup>10.999999</sup>"  "10<sup>10.9999995</sup>" "10<sup>11</sup>"        
# [4] "10<sup>11.0000005</sup>" "10<sup>11.000001</sup>"  
valy2 <- p1$x$layout$yaxis$ticktext %>% 
  toString() %>% str_split(", ") %>%  # <---- that's a comma and whitespace
  unlist() %>% str_replace("\\^(.*)", "<sup>\\1</sup>") %>% 
  str_replace("(\\...).*(</sup>)", "\\1\\2") # wow, just didn't want to do it in one...
# [1] "10<sup>10.99</sup>" "10<sup>10.99</sup>" "10<sup>11</sup>"    "10<sup>11.00</sup>"
# [5] "10<sup>11.00</sup>" 

p1$x$layout$xaxis$ticktext <- valx
p1$x$layout$yaxis$ticktext <- valy2
p1

enter image description here

Now to show you other ways that would work for formatting text if you started in Plotly.

e on the x-axis and r on the y-axis

plot_ly(df1, x = ~x, y = ~y, type = "scatter", mode = "markers") %>% 
  layout(xaxis = list(type = "log", tickformat = ".1e"),
         yaxis = list(type = "log", tickformat = ".1r"))

enter image description here

~s on the x-axis and y on the y-axis

plot_ly(df1, x = ~x, y = ~y, type = "scatter", mode = "markers") %>% 
  layout(xaxis = list(type = "log", tickformat = "~s"),
         yaxis = list(type = "log", tickformat = ".1y"))

enter image description here

  • Related