简体   繁体   English

使用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()

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

which get me a tooltip, but with only seq and value. 这给我一个工具提示,但只有seq和值。 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. 我尝试了选项tooltip =但它指定你可以使用美学中唯一的变量描述,我不会在我的使用中使用该名称。

Any solution? 有解决方案吗 I saw I am not the first with this problem, but I haven't found answer working with ggplotly. 我看到我不是第一个遇到这个问题的人,但我还没有找到与ggplotly合作的答案。

You don't need to modify the plotly object as suggested by @royr2. 您不需要像@ royr2所建议的那样修改plotly对象。 Just add label = name as third aesthetic 只需添加label = name作为第三美学

ggplot(data = d, aes(x = seq, y = value, label = name)) + geom_line() + geom_point()

and the tooltip will display name in addition to seq and value . 除了seqvalue之外,工具提示还会显示name

The ggplotly help file says about tooltip parameter: ggplotly帮助文件说明了tooltip参数:

The default, "all", means show all the aesthetic mappings (including the unofficial "text" aesthetic). 默认情况下,“全部”表示显示所有美学映射(包括非官方的“文本”美学)。

So you can use the label aesthetic as long as you don't want to use it for geom_text . 因此,只要您不想将它用于geom_text您就可以使用label美学。

BTW: I've also tried text instead of label 顺便说一句:我也试过text而不是label

ggplot(data = d, aes(x = seq, y = value, text = name)) + geom_line() + geom_point()

but then ggplot2 complained 但随后ggplot2抱怨道

geom_path: Each group consists of only one observation. geom_path:每组只包含一个观察。 Do you need to adjust the group aesthetic? 你需要调整群体美感吗?

and plotted only points. 并且仅绘制点。 I had to add a dummy group to geom_line to remove the issue: 我不得不在geom_line添加一个虚拟组来删除问题:

ggplot(data = d, aes(x = seq, y = value, text = name)) + geom_line(group = 1) + geom_point()

(But note if you put the dummy group as fourth aesthetic inside aes() it will appear by default also in the tooltip.) (但请注意,如果您将虚拟组放在aes()作为第四美学,它将默认显示在工具提示中。)

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. 但是,如果您想要通过geom_text绘制并在工具提示中显示的不同字符串,我发现非官方text审美可以与label一起使用。

Edit to answer a question in comments: The tooltip parameter to ggplotly() can be used to control the appearance. 编辑以回答评论中的问题: ggplotly()tooltip参数可用于控制外观。 ggplotly(tooltip = NULL) will suppress tooltips at all. ggplotly(tooltip = NULL)将完全禁止工具提示。 ggplotly(tooltip = c("label")) selects the aesthetics to include in the tooltip. ggplotly(tooltip = c("label"))选择要包含在工具提示中的美学。

Building on @UweBlock's answer, you can also create dummy aesthetics in order to display multiple labels in tooltips. 在@UweBlock的答案的基础上,您还可以创建虚拟美学,以便在工具提示中显示多个标签。 I can't find where this is documented, but discovered it emperically. 我找不到记录的位置,但却发现了它的经验。 The dummy variables show up in the order you specify them, but priority is given to the default variables (eg x and y). 虚拟变量按照您指定的顺序显示,但优先级是默认变量(例如x和y)。 To get around this, you can specify those variables in a separate aesthetic, as shown below: 要解决这个问题,您可以在单独的美学中指定这些变量,如下所示:

library(plotly)
p = ggplot(iris, aes(label=Species, label2=Petal.Length, label3=Petal.Width)) + 
  geom_point(aes(Sepal.Length,Sepal.Width))
ggplotly(p)

在此输入图像描述

The unofficial text aesthetic allows you to introduce all the variables you want (here I use name twice to show it): 非官方text审美允许您引入所需的所有变量(这里我使用name两次来显示它):

require(ggplot2)
ggplot(data = d,aes(x = seq, 
                    y = value,
                    group = 1,
                    text = paste('name: ', name,
                                 '</br>name_again: ', name)
                    ))+
  geom_line() + 
  geom_point()

I have to add a dummy group aesthetic for the geom_line to work properly as @UweBlock suggested. 我必须添加一个虚拟group审美,以便geom_line正常工作,如@UweBlock建议的那样。

At last I choose what I want to show in the tooltip (here I excluded group ). 最后我选择了我想在工具提示中显示的内容(这里我排除了group )。

require(plotly)
ggplotly(, tooltip = c("x", "y", "text"))

You'll have to modify the plotly object to do this. 您必须修改plotly object才能执行此操作。 Or use plot_ly() to create the graph instead... 或者使用plot_ly()来创建图形......

EDIT: With the release of plotly 4.0 the syntax will change a little bit. 编辑:随着plotly 4.0的发布,语法会稍微改变一下。

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)

require(plotly)
gg <- ggplot(data = d,aes(x=seq,y=value))+geom_line() + geom_point()

gg <- plotly_build(gg)

#OLD:
gg$data[[1]]$text <- paste("Seq:", d$seq, "<br>",
                           "Value:", d$value, "<br>",
                           "Company:", d$name)

#UPDATED: 
#Plotly_build creates two separate traces:
#One with mode = markers and the other with mode = lines
#Hence modify text for the second trace

gg$x$data[[2]]$text <- paste("Seq:", d$seq, "<br>",
                           "Value:", d$value, "<br>",
                           "Company:", d$name)

gg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM