从绘图中检索轴刻度信息 [英] Retrieve axis tick information from a plotly figure
问题描述
我正在使用R
plotly
绘制热图:
I'm plotting a heatmap using R
plotly
:
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
library(plotly)
library(dplyr)
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label"))
如您所见,plotly
并未显示所有y轴刻度线.我的问题是,是否有可能,如果可以的话,如何检索选择显示的Y轴刻度标签plotly
?
As you can see, plotly
is not showing all y-axis ticks. My question is whether it is possible, and if so how, to retrieve the y-axis tick labels plotly
selected to show?
我保存了绘图对象:
plotly.obj <- plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label"))
然后环顾四周,似乎plotly.obj$x$layoutAttrs
应该存储此信息,但事实并非如此:
And looked around and it seems that perhaps plotly.obj$x$layoutAttrs
should store this information but it doesn't:
> plotly.obj$x$layoutAttrs
$`102ce55fd393e`
$`102ce55fd393e`$yaxis
$`102ce55fd393e`$yaxis$title
NULL
$`102ce55fd393e`$xaxis
$`102ce55fd393e`$xaxis$tickangle
[1] 90
$`102ce55fd393e`$xaxis$tickvals
[1] 10
$`102ce55fd393e`$xaxis$ticktext
[1] "X-Label"
有什么主意吗?
推荐答案
我认为您无法获得刻度线,这些刻度线最终被渲染.但是您可以获得y轴可以选择的所有水平轴.
I don't think you can get the ticks, that are finally rendered. But you can get all the levels of the y-axis, that ploty can choose from.
levels(plotly.obj$x$attrs$`2c4c148651ae`$y)
最终绘制的刻度线是动态选择的,并将根据您的绘图大小等进行调整.
The ticks that are finally rendered are dynamically chosen and will adapt, depending on your plot size etc.
您还可以使用plotly_json()
签出属性:
You can also check out the attributes with plotly_json()
:
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label")) %>%
plotly_json()
这篇关于从绘图中检索轴刻度信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!