Major and minor tickmarks with plotly
Asked Answered
T

2

7

I would like to generate a figure in plotly like the following figure generated with the base R graphics:

R figure with minor ticks

The R code for the figure above is the following:

x = c(1,2,3,4,5)
y = c(0.1, 1, 10, 100, 1000)
axseq = y
plot(x, log10(y), yaxt="n")
axis(2, at=log10(axseq), labels=as.character(axseq))
for (i in 1:5){
    bb = 1:10; a = (bb*10^(i-2));   axis(2, at=log10(a), tcl=-0.25, labels=F)
}

My plotlycode for the same figure so far is the following:

p = plot_ly(x=x, y=log10(y), mode="markers") %>%
layout(yaxis = list(tickmode="array", tickvals=log10(axseq), ticktext=as.character(axseq), zeroline=F, showline=T, ticks="outside"),
       xaxis = list(showline=T, ticks="outside"))

it has the major ticks, but I can't find how to add the minor ticks on the y axis.

Tokoloshe answered 27/6, 2016 at 22:27 Comment(0)
E
7

As you started to implement already, you can customize the ticks by specifying the values at which they are placed (with tickvals) and the labels (with ticktext). But you need to put a value for every tick location in tickvals, and every tickval needs a corresponding ticktext. So, the way to differentiate between major and minor ticks is by setting the ticktext to an empty string for all the minor ticks (because plotly doen't have a way to specify minor ticks per se).

tval <- sort(as.vector(sapply(seq(1,9), function(x) x*10^seq(-1,3)))) #generates a sequence of numbers in logarithmic divisions
ttxt <- rep("",length(tval))  # no label at most of the ticks
ttxt[seq(1,37,9)] <- as.character(tval)[seq(1,37,9)] # every 9th tick is labelled

p = plot_ly(x=x, y=y, mode="markers") %>%
  layout(yaxis = list(type="log",
                      zeroline=F, showline=T, 
                      ticks="outside",
                      tickvals=tval,
                      ticktext=ttxt),
         xaxis = list(showline=T, ticks="outside"))
p

enter image description here

Essayistic answered 27/6, 2016 at 23:40 Comment(5)
Thanks, that's very close to what I'm looking for, but there is an important detail missing: the minor ticks should be shorter than the major ticks. I should be able to control the tick length with the ticklen attribute, but I don't think I can control the length of the major and minor ticks separately.Tokoloshe
No ticklen only takes a single value. You can't set them differentlyEssayistic
I'm working on using your answer dww. Now hoping to find a way to only show gridlines at the labeled ticksAntiseptic
@Antiseptic I have a solution, but its too much for a comment. Suggest asking a new question and linking to this one. If you let me know about the new question (via a comment here), I can answer it for you.Essayistic
@Essayistic i posted it here: #54612642Antiseptic
T
5

I found a solution by adding a trace which is not visible and a second y axis, it's a bit of a hack, but it works:

library(plotly)
x = c(1,2,3,4,5)
y = c(0.1, 1, 10, 100, 1000)
axseq = y

minTickLoc = numeric()
    for (i in 1:5){
    bb = 1:10;
    minTickLoc = c(minTickLoc, (bb*10^(i-2)))
}


p = plot_ly(x=x, y=y, mode="markers", marker=list(opacity=0), showlegend=F) %>%    
   add_trace(x=x, y=y, mode="markers", yaxis="y2", 
             marker = list(color="black")) %>%
   layout(yaxis = list(type = "log", tickvals = axseq, 
                       ticktext = as.character(axseq), 
                       zeroline=F, showline=T, ticks="outside", 
                       ticklen=8, showgrid=T),
          yaxis2 = list(type="log", tickvals=minTickLoc, 
                        ticktext=rep("", length(minTickLoc)), 
                        zeroline=F, showline=F, ticks="outside", 
                        ticklen=3, showgrid=T),
          xaxis = list(showline=T, ticks="outside", ticklen=8))

enter image description here

Tokoloshe answered 29/6, 2016 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.