0

I have generated the following plot, which groups the data based on drug. I then created another data frame which summarized the main data by calculating the mean. These mean values(grouped by drug) are then added to the plot in triangular shapes. Using stat_summary connected the means. Now I would like to know how to add the tooltips for the means data (including x and y values with custom labels) to the plot using ggplotly. The current tooltip only includes the info for the main data

data:

Day,Drug,Sex,Y
1,A,Female,2.192306074
1,B,Male,4.551912798
1,B,Female,1.574070652
1,C,Female,-0.143946163
1,A,Male,5.144422967
1,C,Male,5.724705829
2,A,Male,2.691617258
2,B,Female,-3.0289955
2,C,Male,0.338102762
2,A,Female,-0.558581233
2,B,Female,-2.942620032
2,C,Male,1.024670497
3,A,Male,2.264980803
3,C,Female,2.103722883
3,A,Female,2.091621938
3,B,Male,1.535299922
3,B,Male,1.618399767
3,C,Female,0.136160703

After copying you may need to run the following command to convert it to the dataframe:

df <- read.delim("clipboard", sep = ",")

my variables are in string:

grouping_var <- "Drug"
x <- "Day"
y <- "Y"
color <- "Sex"

Here is the code for that the plot:

text <- paste("X:", df[[grouping_var]], "<br>",
              "Y:", df[[y]], "<br>",
              "group:", grouping_var, "<br>",
              "color:", color)
mean_df <- df %>% group_by(get(x), get(grouping_var)) %>% summarise(Mean = mean(get(y)), .groups = "drop") %>% 
  rename(x = 1, grouping_var = 2)

p <- df %>% ggplot(aes(x = get(x), y = get(y), text = text)) +
  geom_point(aes_string(group = grouping_var, color = color)) +
  stat_summary(fun=mean, geom="line", aes_string(group = grouping_var), color = "red", lty = 2) +
  geom_point(data = mean_df, aes(x = x, y = Mean, group = grouping_var, color = grouping_var),
             pch = 2, size = 3,inherit.aes = F) + 

  xlab(x)+
  ylab(y)

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

The output: enter image description here

1 Answer 1

1

The general approach to achieve your desired result and add a tooltip to the summary points would be to map on the text aes in your second geom_point too. However, IMHO you could simplify your code a bit and as a side effect get rid of the aes_string (which was deprecated in ggplot2 3.4.0 and should therefore be replaced by aes). To this end you could simply rename the original variables as x, y, group and color. Second, I use some small custom functions to create the tooltip texts. Finally, I replaced the stat_summary with a geom_line.

library(ggplot2)
library(plotly)

text1 <- function(x, y, group, color) {
  paste0(
    "X: ", x, "<br>",
    "Y: ", y, "<br>",
    "group (Drug): ", group, "<br>",
    "color (Sex): ", color
  )
}

text2 <- function(x, y, group, color) {
  paste0(
    "X: ", x, "<br>",
    "Y: ", y, "<br>",
    "group (Drug): ", group, "<br>"
  )
}

df <- df %>%
  rename(x = x, y = y, group = grouping_var, color = color) %>%
  mutate(text = text1(x, y, group, color))

mean_df <- df %>%
  group_by(x, group) %>%
  summarise(y = mean(y), .groups = "drop") %>%
  mutate(text = text2(x, y, group, color))

p <- df %>% ggplot(aes(
  x = x, y = y, text = text
)) +
  geom_point(aes(group = group, color = color)) +
  geom_line(
    data = mean_df,
    aes(group = group),
    color = "red", lty = 2
  ) +
  geom_point(
    data = mean_df,
    aes(color = group, text = text),
    pch = 2, size = 3
  ) +
  xlab(x) +
  ylab(y)

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

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

is there any way that the tooltip shows the grouping and colour variables name as well? for example something like this: X:1, Y:3.66, Group(Drug):A, color(Sex): Femal
Sure. Note: I have had another look and found a bug in my code. To fix that I switched back to your mean_df and replaced the stat_summary by a geom_line and geom_point and instead of using one function for the tooltip I use two.
here is my code but is not as efficient as yours in the following way by defining two texts. one for main data and one for the mean: text <- paste("X:", df$x, "<br>", "Y:", df$y, "<br>", "group:(", grouping_var,"):",df$group ,"<br>", "color:(", color,"):",df$color), and here is for the mean based on the mean_df dataframe text2 <- paste("X:", mean_df$x, "<br>", "Y:", mean_df$Mean, "<br>", "group:(", grouping_var,"):",mean_df$group ,"<br>", "color:(", color,"):",mean_df$group)
it seems to be a brilliant idea. but I don't want to hardcode the words' Drug or Sex. I think I can use your function and using paste in the same way I did in the comment above, bu not sure if it works well or I should add more arguments to your functions
Yeah. Basically it's not that important how you add the tooltip texts. And if your code works for you then it's the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.