主要和次要标记带有 [英] Major and minor tickmarks with plotly

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

问题描述

我想在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.因此,区分主要和次要价格变动的方法是将所有次要价格变动的报价文本设置为空字符串(因为密谋没有办法本身指定次要价格变动).

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天全站免登陆