我有一個直方圖,我希望工具提示顯示該箱中的觀察百分比。
這是一個簡單的(可重現的)直方圖:
library(tidyverse)
library(ggplot2)
library(plotly)
hist <- iris %>%
ggplot(aes(x = Sepal.Length))
geom_histogram(bins = 20)
hist %>%
ggplotly(
# This gives hoverover the count and the variable, but I'd like it
# to also have the percent of total observations contained in that bin
tooltip=c("count", "Sepal.Length")
)
除了“count”和“Sepal.Length”,我還想在工具提示中顯示觀察總數的百分比。
例如,最左邊的 bin(包含 4 個觀察值)的值應為 2.7% (4/150)
uj5u.com熱心網友回復:
我會嘗試使用text引數ggplot并設定為計數除以所有計數的總和。使用sprintf您可以獲得所需的格式。在你的tooltip參考text。
library(tidyverse)
library(ggplot2)
library(plotly)
hist <- iris %>%
ggplot(aes(x = Sepal.Length,
text = sprintf("Percent: %0.1f", ..count../sum(..count..) * 100)))
geom_histogram(bins = 20)
hist %>%
ggplotly(
tooltip=c("count", "text", "Sepal.Length")
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/420252.html
標籤:
上一篇:使用rowSums從R中的參考值計算一行中的一定數量的值?
下一篇:R:手動指定因子水平
