2

boxplotヒストグラムの下に配置したい。私はすでにこれを行う方法を考え出しましたが、boxplotと ヒストグラムは同じサイズであり、 をスリムにしたいと考えていboxplotます。幅を小さくすると、端までのスペースは同じままです。しかし、全体の幅を減らしたい。

これが私がこれまでに持っているものです:

library(ggplot2)
h = ggplot(mtcars, aes(x=hp)) +
  geom_histogram(aes(y = ..density..)) +
  scale_x_continuous(breaks=c(100, 200, 300), limits=c(0,400)) +
  geom_density(, linetype="dotted")

b <- ggplot(mtcars,aes(x=factor(0),hp))+geom_boxplot(width=0.1) +
  coord_flip(ylim=c(0,400)) +
  scale_y_continuous(breaks=c(100, 200, 300)) +
  theme(axis.title.y=element_blank(),
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank())

library(gridExtra)
plots <- list(h, b)
grobs <- list()
widths <- list()
for (i in 1:length(plots)){
  grobs[[i]] <- ggplotGrob(plots[[i]])
  widths[[i]] <- grobs[[i]]$widths[2:5]
}
maxwidth <- do.call(grid::unit.pmax, widths)
for (i in 1:length(grobs)){
  grobs[[i]]$widths[2:5] <- as.list(maxwidth)
}
do.call("grid.arrange", c(grobs, ncol=1))

ヒスト+箱ひげ図

編集:
grid.arrange() を次のように使用する場合:

grid.arrange(heights=c(4,1), h, b)

ここに画像の説明を入力

比率は私が望んでいたとおりですが、軸が再び整列するように上記の最初の例を調整する方法がわかりません。

誰?

4

1 に答える 1