带有情节的主要和次要刻度线 [英] Major and minor tickmarks with plotly

查看:22
本文介绍了带有情节的主要和次要刻度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 plotly 中生成一个图形,如下图使用基本 R 图形生成:

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

上图的R代码如下:

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)
}

到目前为止,我的plotly代码如下:

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"))

它有主要刻度,但我找不到如何在 y 轴上添加次要刻度.

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

推荐答案

正如您已经开始实现的那样,您可以通过指定放置刻度的值来自定义刻度(使用 tickvals)和标签(带有 ticktext).但是你需要为 tickvals 中的每个刻度位置设置一个值,并且每个 tickval 都需要一个对应的 ticktext.因此,区分主要和次要刻度的方法是将所有次要刻度的刻度文本设置为空字符串(因为 plotly 没有办法指定次要刻度本身).

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

这篇关于带有情节的主要和次要刻度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆