【问题标题】:How to change axis features in plotly?如何在情节中更改轴特征?
【发布时间】:2017-09-13 05:51:19
【问题描述】:

我在情节中有以下栏,我想:

  1. 让 x 轴标题远离轴标签,以免它们重叠
  2. 将 Y 轴标签放大
  3. 将条形值置于条形顶部

我的代码是:

library(plotly)
plot_ly(x = c('100-200','200-400', '400-600','600-800','800- 1000'),
y = c(12261,29637,17469,11233,17043),          
name = "dd",
type = "bar", 
 xaxis = list(title ="tr"),
yaxis = list(title = "cc") ,                                                
 text = c(12261,29637,17469,11233,17043),textposition = 'auto') %>%
layout(
xaxis = list(tickangle=15, title = " "),
 yaxis = list(title = " "))

感谢您的 cmets :)

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    问题 1:让 x 轴标题远离轴标签,这样它们就不会重叠
    这个问题可以通过在layout 中使用margin = list(b=100, l=100) 设置适当的边距来解决。

    问题 2:将 Y 轴标签放大。
    layout中使用xaxis = list(titlefont=list(size=30))

    问题 3:将条形值置于条形顶部。
    add_texttextposition = 'top' 一起使用

    library(plotly)
    
    x <- c('100-200','200-400', '400-600','600-800','800-1000')
    y <- c(12261,29637,17469,11233,17043)
    labs <- c(12261,29637,17469,11233,17043)
    
    plot_ly(x = x, y = y,          
     name = "dd",
     type = "bar", 
     xaxis = list(title ="tr"),
     yaxis = list(title = "cc")) %>%
    add_text(x=x, y=y, text=labs, hoverinfo='none', textposition = 'top', showlegend = FALSE, 
            textfont=list(size=20, color="black")) %>%
    layout(margin = list(b=100, l=100),
     xaxis = list(tickangle=15, title = "Lenght of exon", titlefont=list(size=30)),
     yaxis = list(title = "Number of genes", titlefont=list(size=30)))
    

    【讨论】:

    • 感谢马可!工作得很好:)
    • 我知道了别担心
    【解决方案2】:

    请注意,您的图表目前不能完全用给定的代码重现,所以如果我做了不必要的假设,请告诉我。

    TLDR:如果您更喜欢问题中描述的所有更改,请查看底部。

    我建议不要使用tickangle,因为它有点搞砸了。试试下面的。注意insidetextfonttickfont 用于yaxis

    library(plotly)
    x = c('100-200','200-400', '400-600','600-800','800- 1000')
    y = c(12261,29637,17469,11233,17043)
    plot_ly(
      x = x, y = y, type = "bar", text = y, textposition = 'auto',
      insidetextfont = list(color = 'white')
    ) %>% layout(
      xaxis = list(title = "Length of exon"),
      yaxis = list(title = "Number of genes", tickfont = list(size = 15))
    )
    

    如果您想让标签位于条形之外,请使用textposition = 'outside' 而不是textposition = 'auto',并去掉默认黑色的insidetextfont。这可能最终会弄乱 y 轴的范围,您需要手动定义范围,这可能很麻烦。

    plot_ly(
      x = x, y = y, type = "bar", text = y, textposition = 'outside'
    ) %>% layout(
      xaxis = list(title = "Length of exon"),
      yaxis = list(
        title = "Number of genes", tickfont = list(size = 15), 
        range = c(0, max(y) + 2000)
      )
    )
    

    这给了我们

    我不推荐tickangle,但如果必须,请在layout 中与margin 一起使用。

    plot_ly(
      x = x, y = y, type = "bar", text = y, textposition = 'outside'
    ) %>% layout(
      xaxis = list(title = "Length of exon", tickangle = 15),
      yaxis = list(
        title = "Number of genes", tickfont = list(size = 15), 
        range = c(0, max(y) + 2000)
      ), margin = list(b=100)
    )
    

    希望这会有所帮助。

    【讨论】:

    • 感谢 Ameya 的全面回答 :)
    • 很高兴它有帮助。如果您认为它对您有帮助,请考虑接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多