Search code examples
rggplot2ggplotly

Where to properly position ggplotly tooltip in ggplot?


When I add a text line to a geom_line plot, the line disappears.

library(tidyverse)
  library("lubridate")
  library(plotly)
  library("RColorBrewer")
  library(htmlwidgets)
  library("reprex")
  
  activity <- c("N", "FB", "N", "N", "N", "FA", "N", "FA", "N", "FA", "N", "N", "N", "N", "N", "FA", "N", "N", "N", "N", "FA", "N", "N", "FA", "FA")
  
  activity_date <- as.Date(c(NA, "2022-04-19", "2022-05-01", "2022-05-01", "2022-05-06", "2022-05-06", "2022-05-07", "2022-05-07", "2022-05-09", "2022-05-09", "2022-05-10", "2022-05-13", "2022-05-14", "2022-05-14", "2022-05-14", "2022-05-15", "2022-05-15", "2022-05-15", "2022-05-15", "2022-05-15", "2022-05-16", "2022-05-16", "2022-05-16", "2022-05-16", "2022-05-16"))
  
  fcrawl_cum <- c(0L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 8L)
  
  clutch_cum <- c(1L, 1L, 2L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 8L, 9L, 10L, 11L, 11L, 12L, 13L, 14L, 15L, 15L, 16L, 17L, 17L, 17L)
  
  turtle_activity_gtm <- tibble(activity, activity_date, fcrawl_cum, clutch_cum)
  
  the_pal <- RColorBrewer::brewer.pal(n = 8,"Dark2") #Set color palette. 

myplot2 <- 
    ggplot() +
    geom_line(data = turtle_activity_gtm,
                aes(x=activity_date, y=fcrawl_cum,
                    text = paste("Date: ", as.Date(activity_date),
                                 "<br>Total: ", fcrawl_cum)),
              na.rm = TRUE,
              linetype = "111111",
                linewidth = 1.5, color = the_pal[6]) +
    geom_line(data = turtle_activity_gtm, 
                aes(x=activity_date, y=clutch_cum), 
              na.rm = TRUE,
              linewidth = 1.5, 
                color = the_pal[7]) +
    labs(title = "myplot2")
  
  myplot2
ggplotly(myplot2)

ggplotly(myplot2, tooltip = c("text"))

If I use, ggplotly(myplot2) the line with the text line added is still not there. However, the data points still appear for missing line. If I use ggplotly with the added tooltip, ggplotly(myplot2, tooltip = c("text")) ,the label is missing for the line without the added text line but the label is exactly as written in the text line.

I would show some of the plots; however, I am not allow to yet. Reputation too low.

How can I do this properly so that both lines show with the added tooltip? I eventually want both lines to have their own text lines added. This is a very simplified chart. One I can get past this problem, I plan to eventually add a lot more items to this chart with a full data set.

Thanks, Jeff


Solution

  • When adding the text attribute to geom_line you have to explicitly set the group aesthetic, i.e. use e.g. group=1 to tell ggplot2 that all observations belong to one group which for simplicity I called 1:

    library(tidyverse)
    library(plotly)
    
    myplot2 <-
      ggplot() +
      geom_line(
        data = turtle_activity_gtm,
        aes(
          x = activity_date, y = fcrawl_cum, group = 1,
          text = paste(
            "Date: ", as.Date(activity_date),
            "<br>Total: ", fcrawl_cum
          )
        ),
        na.rm = TRUE,
        linetype = "111111",
        linewidth = 1.5, color = the_pal[6]
      ) +
      geom_line(
        data = turtle_activity_gtm,
        aes(x = activity_date, y = clutch_cum),
        na.rm = TRUE,
        linewidth = 1.5,
        color = the_pal[7]
      ) +
      labs(title = "myplot2")
    #> Warning in geom_line(data = turtle_activity_gtm, aes(x = activity_date, :
    #> Ignoring unknown aesthetics: text
    
    ggplotly(myplot2, tooltip = c("text"))
    

    EDIT TBMK there is only one text attribute, i.e. specify your tooltip via text the same way as for the first geom_line and use tooltip=c("text").

    But a more ggplot2 like approach to create your chart would be to first reshape your data to long format. Doing so allows to create your plot with just one geom_line but requires map on the color aes, to map on the group aes appropriately and to set your colors via scale_color_manual. Note that doing so will automatically add a legend to your plot:

    turtle_activity_gtm_long <- turtle_activity_gtm %>% 
      tidyr::pivot_longer(c(fcrawl_cum, clutch_cum))
    
    ggplot() +
      geom_line(
        data = turtle_activity_gtm_long,
        aes(
          x = activity_date, y = value, 
          color = name, group = name,
          text = paste(
            "Date: ", as.Date(activity_date),
            "<br>Total: ", value
          )
        ),
        na.rm = TRUE,
        linewidth = 1.5
      ) +
      scale_color_manual(values = c(clutch_cum =  the_pal[[7]], fcrawl_cum = the_pal[[6]])) +
      labs(title = "myplot2")
    
    ggplotly(tooltip = c("text"))
    

    enter image description here