我想在plotly
生成一个数字,就像下图用基础R图形生成一样:
上图中的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
代码如下:
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轴上添加次刻度。
答案 0 :(得分:3)
当您开始实施时,您可以通过指定它们的值(使用tickvals
)和标签(使用ticktext
)来自定义刻度。但您需要为tickvals
中的每个刻度位置设置一个值,并且每个tickval
都需要相应的ticktext
。因此,区分主要和次要刻度的方法是将ticktext设置为所有次要刻度的空字符串(因为在情节上没有办法指定次要刻度本身)。 / p>
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
答案 1 :(得分:2)
我通过添加一条不可见的痕迹和第二个y轴找到了一个解决方案,它有点像黑客,但它有效:
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))