2017-02-13 9 views
0

plotlyで構築されたグラフバーにいくつかのノートを追加するのに助けが必要です。棒グラフでのadd_annotationsの使用方法

私は、次のデータフレーム "" を持っている:

s_0 <- c("NH", "OR", "NE", "PA", "NJ", "MA", "IL", "CA", "WI", "OH", "WV") 
s_1 <- c(0.66719, 0.62480, 0.62797, 0.55121, 0.25582, 0.63407, 0.57009, 0.29712, 0.19979, 0.08913, -0.16944) 
s <- data.frame(s_0, s_1) 

私は、次の棒グラフを生成するためにplot_ly()を使用します。

s_plot <- plot_ly(x = ~s_1, y = reorder(s_0, s_1), 
type = 'bar', 
orientation = 'h', 
marker = list(color = 'rgba(50, 50, 100, 1)', 
line = list(color = 'rgba(128, 0, 128)'))) 

bar chart

を私は追加したいです下の図のように、バーの終わりにあるグラフの "s_1"の値を、すべてのバーに適用します。

enter image description here

私はadd_annotations()を使用していたと仮定し、私はどのように行うのか分かりません。

私は静かにコードを尋ねる、私はそれが非常に簡単でなければならないことを知っている。

答えて

2

あなたのコードの修正とPlotlyによって提供さexampleは、ほぼ右のレイアウトを提供します。

通常、アノテーションはバーの半分になります。xanchorleftに設定しても問題は解決しますが、負の値は見えにくいです。

対処法:

xanchor = ifelse(s_1 > 0, 'left', 'right') 

enter image description here

library(plotly) 

s_0 <- c("NH", "OR", "NE", "PA", "NJ", "MA", "IL", "CA", "WI", "OH", "WV") 
s_1 <- c(0.66719, 0.62480, 0.62797, 0.55121, 0.25582, 0.63407, 0.57009, 0.29712, 0.19979, 0.08913, -0.16944) 
s <- data.frame(s_0, s_1) 

s_plot <- plot_ly(x = ~s_1, y = reorder(s_0, s_1), 
        type = 'bar', 
        orientation = 'h', 
        marker = list(color = 'rgba(50, 50, 100, 1)', 
           line = list(color = 'rgba(128, 0, 128)'))) %>% 
    layout(annotations = list(x = s_1, y = reorder(s_0, s_1), text = s_1, xanchor = ifelse(s_1 > 0, 'left', 'right'), 
          yanchor = 'center', 
          showarrow = FALSE)) 
0

我々は使用することができます。

add_text(s_plot, text = s_1, marker = NULL, textposition = 'right') 

をあなたは異なる属性でプレイすることもできます。

有効な属性は次のとおりです。

'タイプ'、 '目に見える'、 'showlegend' を、'uid'、 'hoverinfo'、 'stream'、 'x'、 'x0'、 'dx'、 'y'、 'y0'、 'dy'、 'legendgroup'、 'opacity'、 'name' 'text'、 'mode'、 'hoveron'、 'line'、 'connectgaps'、 'fill'、 'fillcolor'、'xaxis'、 'yaxis'、 'xsrc'、 'ysrc'、 'textsrc'、 'text'、 'text'、 'text'、 'text'、 'text'、 'text'、 'text'、 'text'、 'text' 'textpositionsrc'、 'RSRC'、 'TSRC'、 'キー'

+0

それは実際にテキストを追加して、しかし、それは完全に正しい位置を欠場。あなたはそれを修正する方法を知っていますか? – smars

関連する問題