R 详细说明如何使用位置减淡和设置悬停文字的格式

R 详细说明如何使用位置减淡和设置悬停文字的格式,r,ggplot2,plotly,r-plotly,R,Ggplot2,Plotly,R Plotly,我想创建一个plotly对象,其1.)位置在x轴上变淡,2.)以我想要的方式格式化文本。我可以分别满足每一个条件,但不能同时满足这两个条件 这里我有一个精心策划的阴谋,是正确的位置回避 library(tidyverse) library(plotly) df <- data.frame( x = c(1, 2, 3, 4), y = runif(8, 5, 10), cat = c("a", "a", "a", &

我想创建一个plotly对象,其1.)位置在x轴上变淡,2.)以我想要的方式格式化文本。我可以分别满足每一个条件,但不能同时满足这两个条件

这里我有一个精心策划的阴谋,是正确的位置回避

library(tidyverse)
library(plotly)

df <- data.frame(
  x = c(1, 2, 3, 4),
  y = runif(8, 5, 10),
  cat = c("a", "a", "a", "a", "b", "b", "b", "b")
)

p <- ggplot(df, groups = cat) +
  geom_point(aes(x = x, y = y, color = cat),
             position = position_dodge(width = 0.3))

ggplotly(p)
库(tidyverse)
图书馆(绘本)

df您可以通过
文本
“美学”传递自定义悬停文本,并将参数
tooltip=“text”
添加到调用
ggplotly

library(tidyverse)
library(plotly)

df <- data.frame(
  x = c(1, 2, 3, 4),
  y = runif(8, 5, 10),
  cat = c("a", "a", "a", "a", "b", "b", "b", "b")
)

p <- ggplot(df, groups = cat) +
  geom_point(aes(x = x, y = y, color = cat, 
                 text = paste('</br> X is ', x, '</br> Y is ', y, '</br> Cat is ', cat)), 
             position = position_dodge(width = 0.3))

ggplotly(p, tooltip = "text")
库(tidyverse)
图书馆(绘本)
df
library(tidyverse)
library(plotly)

df <- data.frame(
  x = c(1, 2, 3, 4),
  y = runif(8, 5, 10),
  cat = c("a", "a", "a", "a", "b", "b", "b", "b")
)

p <- ggplot(df, groups = cat) +
  geom_point(aes(x = x, y = y, color = cat, 
                 text = paste('</br> X is ', x, '</br> Y is ', y, '</br> Cat is ', cat)), 
             position = position_dodge(width = 0.3))

ggplotly(p, tooltip = "text")