2017-09-13 50 views
3

我在plotly以下吧,我想:如何积极改变轴功能?

  1. 得到x轴标题轴标签,好让他们不重叠
  2. 使Y轴标签更大
  3. 带来巴值的吧

顶部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

问题1:获得x轴标题轴标签,好让他们不重叠
这个问题是可以解决的layout设置与margin = list(b=100, l=100)适当的利润率。

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

+0

谢谢Marco!工作得很好:) – Behmah

+0

很高兴知道,但我怎么接受呢? – Behmah

+0

我知道了,别担心 – Behmah

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

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,但是如果必须的话,请使用marginlayout

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

希望这有助于。

+0

感谢Ameya为您全面解答:) – Behmah

+0

高兴它帮助。如果你认为它对你有帮助,请考虑接受答案。 – Ameya