如何在ggplotly的悬停上显示未用于绘图的变量?

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

How to show variables not used for plotting in ggplotly on hover?

问题

假设我在使用ggplot绘图时,有一个数据框中的5个变量。我已经使用了其中的3个变量来绘图。当我悬停在散点图上时,我只看到3个变量的信息。

问题:如何在悬停时显示第4个变量,同时显示其他三个变量?

示例示例

使用鸢尾花数据集:

p1 <- 
  iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(alpha=0.5) 
ggplotly(p1)

如何在悬停时显示Petal.Length的信息?下面的图像显示了当前的悬停显示情况。

英文:

Suppose I have 5 variables in a data frame used for plotting using ggplot.
I have used 3 out of 5 variables for plotting. When I hover over the scatter plot, I only see 3 variables shown.

Question: How can I show variable 4 along with the other three while hovering?

Sample Example

Using Iris Data set:

p1 &lt;- 
  iris %&gt;%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(alpha=0.5) 
ggplotly(p1)

How to get Petal.Length info also displayed while hovering?
Image below shows what is currently being shown on hover.

如何在ggplotly的悬停上显示未用于绘图的变量?

答案1

得分: 1

ggplotly提供了一个名为"text"的"美学"或属性,可以用于在工具提示中显示附加的数据列:

library(plotly)

iris %>%
  ggplot(aes(
    x = Sepal.Length, y = Sepal.Width, color = Species,
    text = paste0("Petal.Length: ", Petal.Length)
  )) +
  geom_point(alpha = 0.5)
ggplotly()

如何在ggplotly的悬停上显示未用于绘图的变量?

英文:

ggplotly offers a text "aesthetic" or attribute which could be used to show additional data columns in the tooltip:

library(plotly)

iris %&gt;%
  ggplot(aes(
    x = Sepal.Length, y = Sepal.Width, color = Species,
    text = paste0(&quot;Petal.Length: &quot;, Petal.Length)
  )) +
  geom_point(alpha = 0.5)
ggplotly()

如何在ggplotly的悬停上显示未用于绘图的变量?

huangapple
  • 本文由 发表于 2023年3月1日 11:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599419.html
匿名

发表评论

匿名网友

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

确定