所以我有一个图,显示了实际值,模型值和错误。下面是相同的代码。
library(plotly)
library(ggplot2)
ab <-tibble::tribble(
~modeled, ~actuals, ~weekenddate, ~err,
501384, 481864, "2014-02-02", 19519,
488933, 479078, "2014-02-09", 9856,
484191, 464026, "2014-02-16", 20165,
480443, 460339, "2014-02-23", 20104,
482512, 464021, "2014-03-02", 18491,
488843, 462458, "2014-03-09", 26385
)
test_bottom <- ggplot(ab, aes(x = weekenddate, y = actuals)) +
geom_smooth(method = "lm", se = FALSE, color = "lightgrey") + # Plot regression slope
geom_segment(aes(xend = weekenddate, yend = modeled), alpha = .2) + # alpha to fade lines
# > Alpha adjustments made here...
geom_point(aes(color = err)) + # Alpha mapped to abs(residuals)
scale_color_gradient2(low = "blue", mid = "white", high = "red") +
guides(color = FALSE) + # Alpha legend removed
geom_point(aes(y = modeled), shape = 1) +
theme_bw()
ggplotly(test_bottom)
<sup>Created on 2018-10-12 by the [reprex package](https://reprex.tidyverse.org) (v0.2.1)</sup>
如果您看到悬停,则其显示的模型值和实际值都相同。悬停应该只显示模型值,而悬停时应该只显示实际值以及错误。红点。
这怎么办?
答案 0 :(得分:1)
一种解决方案是在text
中定义ggplot
美观,然后在ggplotly
中指定必须在工具提示中显示text
。
library(plotly)
library(ggplot2)
ab <-tibble::tribble(
~modeled, ~actuals, ~weekenddate, ~err,
501384, 481864, "2014-02-02", 19519,
488933, 479078, "2014-02-09", 9856,
484191, 464026, "2014-02-16", 20165,
480443, 460339, "2014-02-23", 20104,
482512, 464021, "2014-03-02", 18491,
488843, 462458, "2014-03-09", 26385
)
names(ab)[4] <- "Error"
test_bottom <- ggplot(ab, aes(x = weekenddate, y = actuals,
text=paste0("Date:", weekenddate, "<br>Modeled:", modeled, "<br>Actuals:", actuals))) +
geom_smooth(method = "lm", se = FALSE, color = "lightgrey") +
geom_segment(aes(xend = weekenddate, yend = modeled), alpha = .2) +
geom_point(aes(color = Error)) +
scale_color_gradient2(low = "blue", mid = "white", high = "red") +
guides(color = FALSE) +
geom_point(aes(y = modeled), shape = 1) +
theme_bw()
ggplotly(test_bottom, tooltip=c("text","Error"))