我有连续数据,想要使用R
的plotly
进行箱形图(box plot)
或小提琴图(violin plot)
绘制,但不需要离群值和触须:
set.seed(1)
df <- data.frame(group=c(rep("g1",500),rep("g2",700),rep("g3",600)),
value=c(c(rep(0,490),runif(10,10,15)),abs(rnorm(700,1,10)),c(rep(0,590),runif(10,10,15))),
stringsAsFactors = F)
df$group <- factor(df$group, levels = c("g1","g2","g3"))
我知道如何在 plotly
中删除异常值:
plotly::plot_ly(x = df$group, y =df$value, type = 'box', color = df$group, boxpoints = F, showlegend = F)
但是我仍然有胡须。
我尝试使用ggplot2
来解决这个问题(还限制了y轴的高度,使其不超过第75个百分位):
library(ggplot2)
gp <- ggplot(df, aes(group, value, color = group, fill = group)) + geom_boxplot(outlier.shape = NA, coef = 0) +
scale_y_continuous(limits = c(0, ceiling(max(dplyr::summarise(dplyr::group_by(df, group), tile = quantile(value, probs = 0.75))$tile)))) +
theme_minimal() + theme(legend.position = "none",axis.title = element_blank())
但是将其转换为plotly
对象后,就无法保持该格式:
plotly::ggplotly(gp)
您好,以下是您需要的翻译内容:
有什么想法吗?
希望对您有所帮助。