使用ggplotly时如何选择要在工具提示中显示的变量? [英] How to choose variable to display in tooltip when using ggplotly?
问题描述
我有一个简单的数据框:
I have a simple data frame:
seq <- 1:10
name <- c(paste0("company",1:10))
value <- c(250,125,50,40,40,30,20,20,10,10)
d <- data.frame(seq,name,value)
我想以这种方式绘制它:
And I want to plot it this way:
require(ggplot2)
ggplot(data = d,aes(x=seq,y=value))+geom_line() + geom_point()
现在我想使用 plotly,主要是为了能够在将鼠标悬停在一个点上时获得除值之外的其他信息,例如公司名称.我试试这个:
Now I want to use plotly, mostly to be able, when mousing over a point, to get other information than the value, such as the company name. I try this :
require(plotly)
ggplotly()
这给了我一个工具提示,但只有序列和值.我尝试了选项 tooltip= 但它指定您可以使用美学中描述的唯一变量,而我不在我的 aes 中使用该名称.
which get me a tooltip, but with only seq and value. I tried the option tooltip= but it's specified you can use the only variable describe in the aesthetic, and I don't use the name in my aes.
有什么解决办法吗?我发现我不是第一个遇到这个问题的人,但我还没有找到使用 ggplotly 的答案.
Any solution? I saw I am not the first with this problem, but I haven't found answer working with ggplotly.
推荐答案
您不需要按照@royr2 的建议修改 plotly
对象.只需添加 label = name
作为第三个美学
You don't need to modify the plotly
object as suggested by @royr2. Just add label = name
as third aesthetic
ggplot(data = d, aes(x = seq, y = value, label = name)) + geom_line() + geom_point()
除了seq
和value
之外,工具提示还会显示name
.
and the tooltip will display name
in addition to seq
and value
.
ggplotly
帮助文件说明了 tooltip
参数:
The ggplotly
help file says about tooltip
parameter:
默认值all"表示显示所有美学映射(包括非官方的文本"美学).
The default, "all", means show all the aesthetic mappings (including the unofficial "text" aesthetic).
所以你可以使用 label
美学,只要你不想将它用于 geom_text
.
So you can use the label
aesthetic as long as you don't want to use it for geom_text
.
顺便说一句:我也试过 text
而不是 label
BTW: I've also tried text
instead of label
ggplot(data = d, aes(x = seq, y = value, text = name)) + geom_line() + geom_point()
但随后ggplot2
抱怨
geom_path:每组只包含一个观察.需要调整组审美吗?
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
并且只绘制点.我不得不向 geom_line
添加一个虚拟组来消除这个问题:
and plotted only points. I had to add a dummy group to geom_line
to remove the issue:
ggplot(data = d, aes(x = seq, y = value, text = name)) + geom_line(group = 1) + geom_point()
(但请注意,如果您将虚拟组作为第四个美学元素放在 aes()
中,默认情况下它也会出现在工具提示中.)
(But note if you put the dummy group as fourth aesthetic inside aes()
it will appear by default also in the tooltip.)
但是,我发现如果您希望 geom_text
绘制不同的字符串并显示在工具提示.
However, I find the unofficial text
aesthetic can become useful alongside label
if you want to have different strings plotted by geom_text
and shown in the tooltip.
编辑以回答评论中的问题:ggplotly()
的 tooltip
参数可用于控制外观.ggplotly(tooltip = NULL)
将完全抑制工具提示.ggplotly(tooltip = c("label"))
选择要包含在工具提示中的美学.
Edit to answer a question in comments:
The tooltip
parameter to ggplotly()
can be used to control the appearance. ggplotly(tooltip = NULL)
will suppress tooltips at all. ggplotly(tooltip = c("label"))
selects the aesthetics to include in the tooltip.
这篇关于使用ggplotly时如何选择要在工具提示中显示的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!