我的数据(final.df)如下所示:
A B C Y 1
0 0 0 0 0.05
0 0 1 1 0.03
....基于下面的注释,这里是数据帧的ASCII文本表示。
structure(list(A = c(502, 541, 542, 543, 544, 545, 4304, 4370,
4371, 4372, 4373, 4442), B = c(4.4, 4.2, 4.4, 4.6, 4.8, 5, 5.2,
4.6, 4.8, 5, 5.2, 5.2), C = c(2.6, 2.8, 2.8, 2.8, 2.8, 2.8, 12.6,
12.8, 12.8, 12.8, 12.8, 13), Y = c(1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1), `1` = c(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1), `NA` = c(0,
0, 0, 0, 0, 0, 0, 0, 0.000281600479875937, 0, 0, 0)), .Names = c("A",
"B", "C", "Y", "1", NA), row.names = c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L), class = "data.frame")总而言之,有四列标识每个数据点。我有兴趣根据名称为1的列中的值创建两个箱图。我想比较在列'Y‘中标记为0的点和在列'Y’中标记为1的点的值。最后,我希望能够将鼠标悬停在这些点上以检索元数据,即“A”、“B”、“C”和“1”值。
p <- ggplot(final.df, aes(x = factor(Y), y =
Y, fill = factor(Y)))
p <- p + geom_boxplot() + geom_point() + xlab("Y") + guides(fill =
guide_legend("Y")) + theme(legend.position="top")
final.p <- ggplotly(p) 当前图显示因子(Y)值和1中的相应值。如何在列'A‘、'B’、'C‘中包含元数据?
发布于 2018-08-25 02:04:49
我们可以使用paste0和HTML <br><\br>来构建文本,并指示toolttip使用文本。
p <- ggplot(df, aes(x = factor(Y), y = Y,
fill = factor(Y), text=paste('</br>A: ',A,'</br>B: ',B, '</br>1: ',1)))
ggplotly(p,tooltip = c("text"))发布于 2018-08-25 02:21:54
使用ggplotly的tooltip功能。通过键入help(ggplotly)来阅读它。如下所示:
library(tidyverse)
library(plotly)
set.seed(55)
df <- data.frame(
A = c(rep(0, 8), rep(1, 8)),
B = rep(c(rep(0, 4), rep(1, 4)), 2),
C = rep(c(rep(0, 2), rep(1, 2)), 4),
Y = rep(c(0, 1), 8),
X1 = runif(16)
)
p <- ggplot(df, aes(x = factor(Y), y = X1, fill = factor(Y), A = A, B = B, C = C))
p <- p + geom_boxplot() +
geom_point() +
xlab("Y") +
guides(fill = guide_legend("Y")) +
theme(legend.position = "top")
final.p <- ggplotly(p, tooltip = c("A", "B", "C"))
final.phttps://stackoverflow.com/questions/52009545
复制相似问题