在 R 中绘制对数标度直方图;在工具提示框中显示原始值

Plotting log-scaled histograms in in plotly in R; display original values in tooltip box

我正在使用 R 中的 plotly 包生成直方图,以交互方式检查分布。在某些情况下,我必须应用对数函数以使绘图更易于解释。但是,正如您所期望的那样,在使用 plotly 时,工具提示框中会显示对数标度值。我想要实现的是将原始值放在工具提示框中而不是对数刻度值。

下面是一些示例数据和代码:-

library(tidyverse)
library(plotly)

df <- data.frame(
  sex=factor(rep(c("F", "M"), each=200)),
  weight=round(c(rnorm(200, mean=55, sd=5),
                 rnorm(200, mean=65, sd=5)))
)


p <- ggplot(df, aes(x=weight)) + 
  geom_histogram()+
  scale_x_log10()


ggplotly(p)

这给了你这个结果:-

有人能指出我正确的方向吗?谢谢!

您可以在 ggplot(aes(text = ))paste 您的 weight,并且在 ggplotly(tooltip = c("y", "text")) 中仅包含 texty

p <- ggplot(df, aes(x = weight, text = paste("Weight:", weight))) + 
  geom_histogram() +
  scale_x_log10()

ggplotly(p, tooltip = c("y", "text"))