2017-09-13 92 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