我想将geom_rect与plotly一起使用,但是如果您查看ggplotly(g1),您会看到它没有geom_rect底纹,但是g1有。知道如何让geom_rect与Plotly一起使用吗?
谢谢。
library(plotly)
library(ggplot2)
rects <- data.frame(xstart = as.POSIXct("2018-01-01 12:50:24","GMT"),
xend = as.POSIXct("2018-01-01 12:50:26","GMT")
)
mydata = data.frame( time = as.POSIXct(c("2018-01-01 12:50:23","2018-01-01 12:50:24","2018-01-01 12:50:25","2018-01-01 12:50:26","2018-01-01 12:50:27",
"2018-01-01 12:50:28"),"GMT"),
y = 1:6, z = 101:106, z2= c(6:10,NA) )
g1 =ggplot()+ geom_rect(data = rects, aes(xmin = xstart, xmax = xend,
ymin = -Inf, ymax = Inf), alpha = 0.1)+
geom_point( data = mydata, aes(x = time, y = y,text =
paste0( y ," ",z," ",z2)
))+geom_step(data = mydata, aes(x = time, y = y) )
g1
ggplotly( g1)
答案 0 :(得分:0)
尽管我不确定为什么geom_rect
没有出现在您的绘图中,但是您可以使用geom_polygon
来解决这个问题。
library(plotly)
library(ggplot2)
rects <- data.frame(xstart = as.POSIXct(c("2018-01-01 12:50:24", "2018-01-01 12:50:26",
"2018-01-01 12:50:26","2018-01-01 12:50:24"),"GMT"),
ystart = c(0, 0, 6, 6))
g1 <- ggplot()+
geom_polygon(data = rects, aes(x = xstart, y = ystart), alpha = 0.5)+
geom_point( data = mydata, aes(x = time, y = y))+
geom_step(data = mydata, aes(x = time, y = y))
ggplotly(g1)