ggplotly-object的部分阴影背景 [英] Partially shaded background for ggplotly-object

查看:127
本文介绍了ggplotly-object的部分阴影背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot2创建一个时间序列的线图并将其转换为绘图。这个图的背景的一部分应该以不同的颜色着色(像这样:






  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 =蓝色),不透明度= 0.5,
x0 =1980-01-01,x1 =1990-01-01,
y0 = 6000,y1 = 8000



plot






更新:似乎 plotly`或 gplot``改变了处理日期的方式。

  x0 = 315532800000,x1不是传递字符串日期,而是现在需要传入整数= 631152000000 

即可得到正确的结果。

I'm trying to create a lineplot of a timeseries with ggplot2 and convert it to plotly. A part of the background of this plot is supposed to be shaded in a different color (like this one: Using geom_rect for time series shading in R). Unfortunately annotate() as well as geom_rect aren't transferred to the ggplotly-object as it seems. Therefore I tried to retroactively add a shape using plotly-code (based on this example: https://plot.ly/r/shapes/), but it is not working either, as shown in this reproducible example:

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

So how can I get this shading for my ggplotly-object? Recreating the whole plot in plotly is unfortunately not possible.

Thanks!

解决方案

In order to make ggplotly work with shapes you would need to first clear the shapes coming from the conversion (beats me why they are there in the first place).

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

and you would need to assign the layout function to your plot object.

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


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


Update: It seems that either plotly` orgplot`` changed it's way of handling dates. Instead of passing a string dates, one would need to pass in integers now, i.e. it should be:

x0 = 315532800000, x1 = 631152000000

to get the correct result.

这篇关于ggplotly-object的部分阴影背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆