如何在曲线上改变轴特征?

时间:2017-09-13 05:51:19

标签: r plotly

我有以下条形图,我想:

  1. 使x轴标题远离轴标签,使它们不重叠
  2. 使Y轴标签更大
  3. 将条形值带到条形顶部
  4. enter image description here

    我的代码是:

    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 = " "))
    

    感谢您的评论:)

2 个答案:

答案 0 :(得分:3)

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

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

我建议不要使用tickangle,因为它会使事情变得棘手。请尝试以下方法。请注意insidetextfont使用tickfontyaxis

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))
)

enter image description here

如果您想让标签位于条形图之外,请使用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)
  )
)

这给了我们enter image description here

我不建议使用tickangle,但如果必须,请将其与margin中的layout一起使用。

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)
)

enter image description here

希望这有帮助。

答案 1 :(得分:3)

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

问题2:使Y轴标签更大 在xaxis = list(titlefont=list(size=30))中使用layout
问题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)))

enter image description here