用R中的直方图叠加箱线图

TOI-lab9

我有以下代码,

d <- mtcars

library(plotly)
# box on top / hist bottom
boxplot<-plot_ly(d, x = ~mpg, type = "box", name = "mpg")
histogram<-plot_ly(d, x = ~mpg, type = "histogram", name = "mpg")

我想将箱线图放在直方图的顶部,如下图所示:

在此处输入图片说明

我尝试使用 subplot() R 函数,但没有得到所需的结果

subplot(boxplot,histogram) %>%   layout(showlegend=FALSE)

提前致谢!

巴斯蒂安·杜克勒

您有shareX共享相同 X 轴的nrows参数垂直放置它参数:

subplot(boxplot,histogram,nrows=2,shareX=TRUE) %>%   layout(showlegend=FALSE)

如果您设置,您还可以消除线条:

ax <- list(
  showgrid = FALSE
)
boxplot<-plot_ly(d, x = ~mpg, type = "box", name = "mpg")%>%  
  layout(showlegend=FALSE,xaxis = ax,yaxis = ax)
histogram<-plot_ly(d, x = ~mpg, type = "histogram", name = "mpg")%>%  
  layout(showlegend=FALSE,xaxis = ax,yaxis = ax)

subplot(boxplot,histogram,nrows=2,shareX=TRUE)

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章