【问题标题】:Bars beginning at 1 instead of 0从 1 而不是 0 开始的条形图
【发布时间】:2017-06-05 21:25:17
【问题描述】:

我正在尝试构建一个带有对数轴的条形图,但我希望条形图从 1 而不是 0 开始。到目前为止,我有一个基本的条形图,

df <- data.frame(Categ=c("a","b","a","a","b"),Num=c(1,2,3,4,5),Value=c(1.56,0.85,10.6,8.9,32.8))
plot_ly(data=df,x=~Num,y=~Value,type="bar",color=~Categ) %>%
layout(yaxis=list(exponentformat='power',type='log',title=ylab),legend=list(orientation='h'))

我可以添加白条,所以一切都从 1 开始:

plot_ly(data=df,x=~Num,y=~rep(1,5),type="bar",opacity=0.0,showlegend=FALSE) %>%
add_trace(y=~(Value-1),opacity=1.0,color=~Categ,showlegend=TRUE) %>%
layout(yaxis=list(exponentformat='power',type='log'),legend=list(orientation='h'),barmode='stack')

这里的问题是,如果你双击图例,只显示类别 b,那么白条也会消失,任何低于 1 的值都会消失。 我想知道这个问题是否有更好的解决方案,或者有一种方法可以使白条永久化,以便始终显示小于 1 的值。

【问题讨论】:

  • 条形图和对数刻度没有多大意义,因为 y 轴没有零。相对条形高度可能会失真,具体取决于您开始图表时接近零的程度。

标签: r plotly


【解决方案1】:

在浏览了一堆文档后,我找到了解决方法... https://plot.ly/r/reference/ 如果您使用 base 命令,它会将 base 设置为您需要的任何内容。

plot_ly(data=df,x=~Num,y=~(Value-1),base=1,type="bar",color=~Categ,showlegend=TRUE) %>%
layout(yaxis=list(exponentformat='power',type='log'),legend=list(orientation='h'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多