我正在try 将ggplot2图形转换为plotly图形,这似乎工作得很好,但是鼠标悬停在上面时,Tumor_Volume变量的值显示为NA.它还显示了两次组变量: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)

不知道怎么解决这个问题,有什么帮助吗?

编辑:因我忘了而索取的样本数据,抱歉

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只是小鼠,重复测量总共四只

group是群变量,两个群;A和B

1.00为基线转化tumor_volume个肿瘤体积

weight个归一化权重值,第1天为0,然后查看后续天数的权重增量

EDIT2:dput(mouse_data)个输出:

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"))

推荐答案

stat_summary在内部计算聚合,因此在本例中(因为您使用的是ggplotly(p)并希望显示聚合),我建议在绘制之前显式汇总,然后使用geom_linegeom_point而不是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)

或者在不创建新数据集的情况下:

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)

无论哪种方式,你都会得到:

enter image description here

R相关问答推荐

如何使用shinyChatR包配置聊天机器人

R箱形图gplot 2 4组但6个参数

如何求解arg必须为NULL或deSolve包的ode函数中的字符向量错误

如何使用按钮切换轨迹?

如何将dygraph调用到R Markdown作为一个shiny 的react 对象的参数?

筛选出以特定顺序患病的个体

删除列表中存储的数据帧内和数据帧之间的重复行

bslib::card_header中的shine::downloadButton,图标而不是文本

未识别时区

根据列表中项目的名称合并数据框和列表

按列中显示的配对组估算NA值

如何将一个方阵分解成没有循环的立方体

'使用`purrr::pwalk`从嵌套的嵌套框架中的列表列保存ggplots时出现未使用的参数错误

删除字符串R中的重复项

是否有一个R函数可以输出在输入的字符向量中找到的相应正则表达式模式?

如何移动点以使它们的打印不重叠

如何将宽格式的患者信息数据高效地转换为患者计数的时间序列?

R将函数参数传递给ggploy

使用列名和r中的前缀 Select 列的CREATE函数

如何在不使用SHINY的情况下将下拉滤镜列表添加到ggploy?