2016-12-06 39 views
1

私は(いくつかのヶ月)red horizontal line(取得するゲイン)を持っていると思われる、bar plotplotlyとパッケージ)を作成したいと思います。下のプロットは私の問題をより正確に示しています。水平ラインはplotly、

enter image description here

コードと最初のプロットを得るために必要なデータ:

library("plotly") 
library("dplyr") 

data.frame(miesiac_label = as.character(as.roman(c(1:12))), 
      miesiac = c(1:12), 
      ile = c(12000, 12100, 11100, 12000, 12000, 11900, 12200, 12100, 6000, 12100, 12100, 12100), 
      gain = c(rep(NA, 7), 11000, 12000, 12000, 12000, 12000)) -> dane 
dane$miesiac_label <- factor(dane$miesiac_label, levels = dane[["miesiac_label"]]) 

plot_ly(dane) %>% 
    add_trace(x = ~miesiac_label, y = ~ile, 
       type = 'bar', marker = list(color = '#99d3df')) %>% 
    add_trace(x = ~miesiac_label, y = ~gain, name = 'Gain', 
       type = "scatter", mode='lines+markers', marker = list(color = 'red'), 
       line = list(color = 'red')) 

私はこれを行うには継続的な規模を持っているし、この後にちょうどx axis labelsを変更する必要があることだと思うが、私は方法がわかりませんそれらのラベルを変更する(私はもちろん、Googleでそれを最初に見つけようとしている)...

ありがとうございました!

+0

あなたのコードは私にとって空のプロットですが、正しいのでしょうか? –

+0

@MarijnStevering、はい、私はもう一度あなたのためにそれをチェックしました - それは正しいです。 – Marta

+0

ああ、更新私の陰謀、それは私の悪い。 –

答えて

1

あなたのために働くようなものでしょうか?あなたはadd_segmentsの数字を調整することができます。

a <- list(
    title = "miesiac_label", 
    showticklabels = TRUE, 
    tickmode= "array", 
    ticktext = as.character(as.roman(c(1:12))), 
tickvals = c(1:12) 
) 

plot_ly(dane) %>% 
    add_bars(x = ~miesiac, y = ~ile) %>% 
    add_segments(x = 7.5, xend = 8.5, y = 10000, yend = ~10000, line = list(dash = "dash")) %>% 
    add_segments(x = 8.5, xend = 12.5, y = 12000, yend = ~12000, line = list(dash = "dash")) %>% 
    layout(showlegend = FALSE, xaxis = a) 
1

は、私はあなたがホバーに恐ろしいツールチップにつながるggplot規格のため、通常はそれをやってggplot、素晴らしいggplotly()

を使用して欲しいものを構築するために管理しているが、それはtext美的とでtooltip引数で微調整することができます例えばggplotlyコール

次のプロットになり
ggplot(dane, aes(x = miesiac_label, y = ile)) + 
    geom_bar(aes(text = paste("x:", miesiac_label, "y:",ile)), 
          stat = "identity", fill = "#99d3df") + 
    geom_segment(aes(x = miesiac - 0.5, xend = miesiac + 0.5, 
        y = gain, yend = gain, 
        text = paste0("gain: ",gain)) 
       , colour = "red" 
       , linetype = 2) 

ggplotly(tooltip = "text") 

enter image description here

+0

ありがとうございましたが、実際には 'ggplot2'でそれを行う方法を知っています:)そして、' ggplotly() 'を使わない理由がいくつかありました。しかし、私は本当にそれらを覚えていません:)私はいくつかの 'プロット'の解決策を探しています。 – Marta

+0

ああ、私の助産師の経験はすべて 'ggplotly'ですから –

+0

ありがとう、とにかく:) – Marta