簡體   English   中英

用R中的直方圖疊加箱線圖

[英]Overlaying boxplot with histogram in R

我有以下代碼,

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)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM