gpt4 book ai didi

R:悬停文本的 ggplot

转载 作者:行者123 更新时间:2023-12-02 03:30:19 25 4
gpt4 key购买 nike

我的数据 (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”列中包含元数据?

最佳答案

我们可以使用 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"))

关于R:悬停文本的 ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52009545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com