Не решение ggplotly
, а решение plotly
. (По крайней мере, на мой взгляд, ggplotly
хорошо, если вы хотите создать быструю интерактивную версию ggplot. Однако, ggplotly
все еще имеет много проблем и не может конвертировать каждый ggplot. Попробуйте это:
library("ggplot2")
library("plotly")
test_data <- data.frame(
A = c(1, 5, 7, 4, 2),
B = c(3, 3, 6, 8, 4)
)
my_days <- as.Date(c(
"2010-01-01", "2010-02-01",
"2010-03-01", "2010- 4-01",
"2010-05-01"
))
df <- data.frame(test_data, my_days)
# Anotate Box
s_1 <- unique(min(df$my_days))
s_2 <- unique(max(df$my_days))
target <- 1
p <- df %>%
group_by(my_days) %>%
summarise(prop = sum(A / B)) %>%
plot_ly(x = ~my_days, y = ~prop) %>%
add_lines(
line = list(color = "purple"),
hoverinfo = "text",
text = ~ paste0(
"mydays: ", my_days,
"\n", "prop: ", round(prop, 7)
)) %>%
# Add the rectangles and set x-axis as in ggplot
layout(
xaxis = list(
type = "date",
tickformat = "%b",
nticks = 5
),
shapes = list(
list(
type = "rect",
fillcolor = "red", opacity = 0.2,
x0 = s_1, x1 = s_2, xref = "x",
y0 = -Inf, y1 = target, yref = "y"
),
list(
type = "rect",
fillcolor = "green", opacity = 0.2,
x0 = s_1, x1 = s_2, xref = "x",
# Setting y1 to Inf results in a yaxis which spans up to 2.5. So I chose 1.8 to mimic the ggplot
y0 = target, y1 = 1.8, yref = "y"
)
)
)
p
![enter image description here](https://i.stack.imgur.com/tln95.png)
Создано в 2020-04-05 пакетом Представить (v0.3.0)