在折线图的特定位置添加点或点,并使用 ggplotly() 相应地调整工具提示

Add points or dots in specific positions of a line chart and adapt the tooltip accordingly with ggplotly()

我创建了下面的总数据框:

# Dummy data
data <- data.frame(
  int_end = as.Date("2017-06-14") - 0:364,
  users = runif(365) + seq(-140, 224)^2 / 10000,
  user_type=sample(c('active', 'inactive'), 365, replace = TRUE)
)

data2 <- data.frame(
  int_end = as.Date("2017-06-12") - 0:12,
  MP =sample(c('P', 'M'), 13, replace = TRUE),
  DESCRIPTION=sample(c('text1', 'text2','text3'), 13, replace = TRUE)
  
)
# merge two data frames by ID
total <- merge(data,data2,by="int_end",all = TRUE)

我用 ggplotly() 创建了一个折线图。我想要实现的是在 MPDESCRIPTION 列中有数据的折线图中添加点或点或气泡。此外,这些点的工具提示除了其他列外还应包括 MPDESCRIPTION。在其余点 MPDESCRIPTION 理想情况下根本不应该显示在工具提示中,或者至少应该像 NAs,例如 DESCRIPTION:NA

library(plotly)
plot <- total %>%
  ggplot(aes(int_end, users, color = user_type)) +
  geom_line() +
  theme_bw() +
  #theme(legend.position = "none")+
  theme(legend.position = 'top')+
  labs(title = glue::glue("Number of Users over time."),
       subtitle = glue::glue("Interval window of days."),
       y = "", x = "")
ggplotly(plot)

这可以这样实现:

  1. 对于点,您可以添加一个 geom_point 层,您可以在该层中传递经过过滤的数据集,其中仅包含未缺失 MPDESCRIPTION
  2. 的观测值
  3. 条件工具提示可以通过 text aes 实现。为了方便起见,我通过一个单独的函数创建工具提示文本,并将工具提示文本作为新列添加到您的数据集中。
# merge two data frames by ID
total <- merge(data,data2,by="int_end",all = TRUE)

tooltip_text <- function(int_end, users, user_type, MP, DESCRIPTION) {
  text <- glue::glue("int_end: {int_end}", "<br>",
                     "users: {users}", "<br>",
                     "user_type: {user_type}")
  
  text <- ifelse(!is.na(MP), glue::glue("{text}<br>MP: {MP}"), text)
  text <- ifelse(!is.na(DESCRIPTION), glue::glue("{text}<br>DESCRIPTION: {DESCRIPTION}"), text)
  
  text
}
library(plotly)
library(dplyr)

total <- mutate(total, text = tooltip_text(int_end, users, user_type, MP, DESCRIPTION))

plot <- total %>%
  ggplot(aes(int_end, users, color = user_type, group = user_type,
             text = text)) +
  geom_line() +
  geom_point(data = filter(total, !is.na(MP) | !is.na(DESCRIPTION))) +
  theme_bw() +
  theme(legend.position = 'top')+
  labs(title = glue::glue("Number of Users over time."),
       subtitle = glue::glue("Interval window of days."),
       y = "", x = "")
ggplotly(plot, tooltip = "text")