2017-04-20 5 views
1

私はggplot2でtimeseriesのラインプロットを作成し、それをplotlyに変換しようとしています。このプロットの背景の一部は、別の色(このようなもの:Using geom_rect for time series shading in R)で網掛けされています。残念ながら、annotate()およびgeom_rectはggplotlyオブジェクトには転送されません。したがって、私は、さかのぼって(この例に基づいて:https://plot.ly/r/shapes/)plotlyコードを使用して図形を追加しようとしましたが、この再現性の例に示すように、それは、どちらか動作していない:ggplotly-objectの部分的に影付きの背景

library(plotly) 
library(ggplot2) 

plot <-ggplot(data = economics, aes(x = date, y = unemploy)) + geom_line() 

plot <- ggplotly(plot) 

layout(plot, 
     shapes = list(
     list(type = "rect", 
       fillcolor = "blue", line = list(color = "blue"), opacity = 0.9, 
       x0 = "1980-01-01", x1 = "1990-01-01", 
       y0 = 0, y1 = 4000 
     ) 
     ) 
) 

は、どのように私は、このシェーディングを得ることができます私のggplotlyオブジェクト?プロット全体を再現することは残念ながら不可能です。

ありがとうございます!

答えて

0

ggplotlyを図形で処理するには、最初にコンバージョンに由来する図形を消去する必要があります

plot[['x']][['layout']][['shapes']] <- c() 

、あなたのplotオブジェクトにlayout機能を割り当てる必要があります。

plot <- layout(plot, 
     shapes = list(
     list(type = "rect", 
       fillcolor = "blue", line = list(color = "blue"), opacity = 0.9, 
       x0 = "1980-01-01", x1 = "1990-01-01", 
       y0 = 0, y1 = 4000 
     ) 
     ) 
) 

enter image description here


library(plotly) 
library(ggplot2) 

plot <-ggplot(data = economics, aes(x = date, y = unemploy)) + geom_line() 
plot <- ggplotly(plot) 

plot[['x']][['layout']][['shapes']] <- c() 

plot <- layout(plot, 
     shapes = list(
     list(type = "rect", 
       fillcolor = "blue", line = list(color = "blue"), opacity = 0.5, 
       x0 = "1980-01-01", x1 = "1990-01-01", 
       y0 = 6000, y1 = 8000 
     ) 
     ) 
) 
plot 

更新plotly` or gplot``のいずれかが、それは日付を処理する方法です変わっているようです。文字列の日付を渡す代わりに、整数を渡す必要があります。つまり、正しい結果を得るには、

x0 = 315532800000, x1 = 631152000000 

とする必要があります。

+0

これは信じられないほど、私はそれを考えなかったでしょう。どうもありがとうございます! – aeq

関連する問題