使用 r plotly 对异常值进行箱线图注释
boxplot annotation to outliers using r plotly
使用下面的鸢尾花数据集,我如何在异常值悬停时获取花的 ID
library(plotly)
?
我试过类似的方法:
iris_ids <- iris %>%
mutate(id = rownames(iris))
plot_ly(iris, y = ~Sepal.Length, x= ~Species, type = 'box') %>%
layout(title = 'Box Plot',
xaxis = list(title = "cond", showgrid = F),
yaxis = list(title = "rating"),
annotations = list(
x = boxplot.stats(Species)$out,
# use boxplot.stats() to get the outlier's y coordinate
y = boxplot.stats(Sepal.Length)$out,
# I want the ID of the flower
# of the outliers
text = c("ID:", id),
showarrow = FALSE,
xanchor = "right"
)
) %>%
config(displayModeBar = FALSE)
并且还尝试使用 ggplotly 包装器:
ggplotly(
ggplot(iris_id, aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
) %>%
#....what goes here....
我更喜欢第二种方式,因为我更喜欢 ggplot2 中的主题,但我愿意接受任何和所有建议!!谢谢。
试试这个方法,确保您可以进一步自定义:
library(ggplot2)
library(plotly)
library(dplyr)
#Data
iris_ids <- iris %>%
mutate(id = rownames(iris))
#Plot
gg <- ggplotly(
ggplot(iris_ids, aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
)
hoverinfo <- with(iris_ids, paste0("id: ", id, "</br></br>",
"Sepal.Length: ", Sepal.Length, "</br>"))
gg$x$data[[1]]$text <- hoverinfo
gg$x$data[[1]]$hoverinfo <- c("text", "boxes")
gg
输出:
使用下面的鸢尾花数据集,我如何在异常值悬停时获取花的 ID
library(plotly)
?
我试过类似的方法:
iris_ids <- iris %>%
mutate(id = rownames(iris))
plot_ly(iris, y = ~Sepal.Length, x= ~Species, type = 'box') %>%
layout(title = 'Box Plot',
xaxis = list(title = "cond", showgrid = F),
yaxis = list(title = "rating"),
annotations = list(
x = boxplot.stats(Species)$out,
# use boxplot.stats() to get the outlier's y coordinate
y = boxplot.stats(Sepal.Length)$out,
# I want the ID of the flower
# of the outliers
text = c("ID:", id),
showarrow = FALSE,
xanchor = "right"
)
) %>%
config(displayModeBar = FALSE)
并且还尝试使用 ggplotly 包装器:
ggplotly(
ggplot(iris_id, aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
) %>%
#....what goes here....
我更喜欢第二种方式,因为我更喜欢 ggplot2 中的主题,但我愿意接受任何和所有建议!!谢谢。
试试这个方法,确保您可以进一步自定义:
library(ggplot2)
library(plotly)
library(dplyr)
#Data
iris_ids <- iris %>%
mutate(id = rownames(iris))
#Plot
gg <- ggplotly(
ggplot(iris_ids, aes(x = Species, y = Sepal.Length)) +
geom_boxplot()
)
hoverinfo <- with(iris_ids, paste0("id: ", id, "</br></br>",
"Sepal.Length: ", Sepal.Length, "</br>"))
gg$x$data[[1]]$text <- hoverinfo
gg$x$data[[1]]$hoverinfo <- c("text", "boxes")
gg
输出: