Plotly在悬停时显示缺失值

huangapple go评论83阅读模式
英文:

Plotly showing NA for values in hover over

问题

我正在尝试将ggplot2图形转换为plotly图形,似乎工作正常,但悬停时显示Tumor_Volume变量的值为NA。它还显示组变量两次:悬停截图

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)

不确定如何修复这个问题,有任何帮助吗?

EDIT:由于我忘记提供样本数据,请求样本数据的代码如下:

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

Tumor_Volume是肿瘤体积,以1.00作为基线进行转换

Weight是标准化的体重值,以第1天为0,然后查看后续天数的体重变化

EDIT2:dput(mouse_data) 输出如下:(此部分文本太长,无法全部显示,请使用代码示例中的数据代替。)

英文:

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 &lt;- as.factor(mouse_data$Mouse)
mouse_data$Group &lt;- as.factor(mouse_data$Group)

p &lt;- ggplot(mouse_data, aes(x=Day, y=Tumor_Volume, color=Group,)) + 
      stat_summary(aes(group=Group), fun=mean, geom=&quot;line&quot;, size=1) +
      stat_summary(aes(group=Group), fun=mean, geom=&quot;point&quot;, size=2) +
      stat_summary(fun.data = mean_se, geom=&#39;errorbar&#39;, 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 &lt;- c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)
Group &lt;- c(&#39;A&#39;,&#39;A&#39;,&#39;A&#39;,&#39;B&#39;,&#39;B&#39;,&#39;B&#39;,&#39;A&#39;,&#39;A&#39;,&#39;A&#39;,&#39;B&#39;,&#39;B&#39;,&#39;B&#39;)
Day &lt;- c(1, 3, 5, 1, 3, 5, 1, 3, 5, 1, 3, 5)
Tumor_Volume &lt;- c(1, 1.0829917, 1.2260616, 1, 1.1432288, 1.3644288, 
                  1, 1.0551514, 1.3203255, 1, 1.3336704, 1.5578587)
Weight &lt;- c(0, -1, -1, 0, 0, 1, 0, 0, 0, 0, 0, -1)

mouse_data &lt;- 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(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, 
&quot;4&quot;, &quot;5&quot;, &quot;6&quot;), class = &quot;factor&quot;), 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(&quot;Ionis-2&quot;, 
&quot;PBS&quot;), class = &quot;factor&quot;), 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(&quot;tbl_df&quot;, 
&quot;tbl&quot;, &quot;data.frame&quot;))

答案1

得分: 0

`stat_summary`在内部计算汇总信息,所以在这种情况下(因为您正在使用`ggplotly(p)`并且想要显示汇总信息),我建议在绘图之前明确汇总,然后使用`geom_line`和`geom_point`代替`stat_summary`:

library(dplyr)
mouse_plot <- mouse_data %>%
summarize(TV_mean = mean(Tumor_Volume),
.by = c(Day, Group))

或者对于较旧版本的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][1]][1]
英文:

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 &lt;- mouse_data %&gt;%
  summarize(TV_mean = mean(Tumor_Volume), 
            .by = c(Day, Group))

## or for older versions of dplyr:
mouse_plot &lt;- mouse_data %&gt;%
  group_by(Day, Group) %&gt;%
  summarize(TV_mean = mean(Tumor_Volume))


p &lt;- 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 &lt;- mouse_data %&gt;%
  summarize(Tumor_Volume = mean(Tumor_Volume), 
            .by = c(Day, Group)) %&gt;%
  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:

Plotly在悬停时显示缺失值

huangapple
  • 本文由 发表于 2023年7月18日 02:32:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定