Search code examples
rggplot2graphstatisticsplotly

Plotly showing NA for values in hover over


I"m trying to convert a ggplot2 graph into a plotly graph, which seems to be working fine however the hover-over shows the values for the Tumor_Volume variable as NA. It also shows the group variable twice: Screenshot of hover-over.

library(ggplot2)
library(plotly)

mouse_data$Mouse <- as.factor(mouse_data$Mouse)
mouse_data$Group <- as.factor(mouse_data$Group)

p <- ggplot(mouse_data, aes(x=Day, y=Tumor_Volume, color=Group,)) + 
      stat_summary(aes(group=Group), fun=mean, geom="line", size=1) +
      stat_summary(aes(group=Group), fun=mean, geom="point", size=2) +
      stat_summary(fun.data = mean_se, geom='errorbar', width=.5, size=0.7) +
      theme_classic() 

p

ggplotly(p)

Not sure how to fix this, any help?

EDIT: Sample data requested due to me forgetting, apologies

Mouse <- c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)
Group <- c('A','A','A','B','B','B','A','A','A','B','B','B')
Day <- c(1, 3, 5, 1, 3, 5, 1, 3, 5, 1, 3, 5)
Tumor_Volume <- c(1, 1.0829917, 1.2260616, 1, 1.1432288, 1.3644288, 
                  1, 1.0551514, 1.3203255, 1, 1.3336704, 1.5578587)
Weight <- c(0, -1, -1, 0, 0, 1, 0, 0, 0, 0, 0, -1)

mouse_data <- data.frame(Mouse, Group, Day, Tumor_Volume)

mouse is mouse number, four mice total with repeated measures

group is the group variable, two groups; A and B

tumor_volume tumor volume transformed with 1.00 as baseline

weight normalized weight values with day 1 at 0 and then looking at delta in weight for further days

EDIT2: dput(mouse_data) output:

structure(list(Mouse = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), levels = c("1", "2", "3", 
"4", "5", "6"), class = "factor"), Group = structure(c(2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("Ionis-2", 
"PBS"), class = "factor"), Day = c(1, 3, 5, 7, 9, 11, 13, 15, 
17, 19, 21, 23, 25, 27, 29, 31, 1, 3, 5, 7, 9, 11, 13, 15, 17, 
19, 21, 23, 25, 27, 29, 31, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 
21, 23, 25, 27, 29, 31, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 
23, 25, 27, 29, 31, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 
25, 27, 29, 31, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 
27, 29, 31), Tumor_Volume = c(1, 1.08299168, 1.22606163, 1.28005149, 
1.15473202, 1.77751867, 1.7775596, 1.42810373, 1.49398489, 1.73608742, 
1.9960584, 2.41555584, 2.44329879, 2.83244784, 2.95047186, 3.13712572, 
1, 1.14322875, 1.36442879, 1.62265073, 1.41229115, 1.69515171, 
1.85693702, 2.00477197, 2.45190977, 2.81710137, 2.95579987, 3.24677192, 
3.7952463, 4.672606, 4.65405432, 5.04395284, 1, 1.08678546, 1.4051892, 
1.70719996, 1.91914965, 2.77444381, 2.91922649, 3.12429506, 3.08423878, 
3.1993417, 3.79480258, 3.45039433, 3.97340486, 4.40332097, 4.99459949, 
5.19001667, 1, 1.05515139, 1.32032553, 1.31451857, 1.36498317, 
1.48691696, 1.71555664, 1.69085447, 1.40778153, 1.16562843, 1.43153217, 
1.14711724, 1.00752507, 0.93548112, 0.89387479, 0.5831787, 1, 
1.33367041, 1.55785873, 1.97875617, 2.58821435, 2.66654602, 3.64662179, 
3.06141446, 2.84366305, 2.56583405, 2.74313995, 2.31355113, 2.2229406, 
2.18822338, 1.80662598, 1.59483541, 1, 1.59580807, 1.92068763, 
2.1997625, 2.03248152, 2.45503449, 2.5517737, 2.57760473, 2.40887638, 
3.15143769, 3.01258129, 2.79074441, 2.5628939, 2.86507289, 2.76465271, 
2.45859679), Weight = c(0, -1, -1, -1, -1, -2, -1, -1, -2, -1, 
-1, -2, -2, -2, -2, -2, 0, 0, 1, 1, 0, 0, 0, 0, -1, -2, -1, -1, 
-1, -2, -2, -1, 0, 0, 0, 0, 0, -1, -1, -1, 0, -1, -1, -1, -1, 
-1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3, 3, 2, 2, 2, 0, 
0, 0, 0, 0, -1, -1, 1, 0, 1, 3, 3, 2, 3, 3, 3, 0, -1, 0, 0, 0, 
0, 0, 0, 0, 0, 1, 1, 1, 2, 1, 1)), row.names = c(NA, -96L), class = c("tbl_df", 
"tbl", "data.frame"))

Solution

  • stat_summary internally computes aggregates, so in this case (since you are using ggplotly(p) and want to show the aggregates) I would suggest explicitly summarizing before plotting, then using geom_line and geom_point instead of stat_summary:

    library(dplyr)
    mouse_plot <- mouse_data %>%
      summarize(TV_mean = mean(Tumor_Volume), 
                .by = c(Day, Group))
    
    ## or for older versions of dplyr:
    mouse_plot <- mouse_data %>%
      group_by(Day, Group) %>%
      summarize(TV_mean = mean(Tumor_Volume))
    
    
    p <- ggplot(mouse_plot, aes(x = Day, y = TV_mean, color = Group)) + 
      geom_line(size = 1) + 
      geom_point(size = 2) +
      theme_classic()
      
    ggplotly(p)
    

    Or without creating a new dataset:

    p <- mouse_data %>%
      summarize(Tumor_Volume = mean(Tumor_Volume), 
                .by = c(Day, Group)) %>%
      ggplot(aes(x=Day, y=Tumor_Volume, color=Group)) + 
      geom_line(size = 1) + 
      geom_point(size = 2) +
      theme_classic()
    
    ggplotly(p)
    

    Either way you get:

    enter image description here