LoginSignup
14

More than 5 years have passed since last update.

複数のhistogramを重ねて描きたい

Last updated at Posted at 2015-07-28

Q.

複数のhistogramを重ねて1枚の図の中に描きたいです。density plotだとうまくいくんですけど、histogramだと以下みたいに積み上げ棒グラフになっちゃってうまくいかないんです。

NG_example.R
library(ggplot2)

ggplot(data=iris, aes(x=Sepal.Length, fill=Species)) + geom_histogram(alpha=0.4)

NG.png

A.

geom_histogramのデフォルトのpositionがstackなのが原因です。
http://docs.ggplot2.org/current/geom_histogram.html
identityを指定することで望みの図を得ることができます。

OK_example.R
library(ggplot2)

ggplot(data=iris, aes(x=Sepal.Length, fill=Species)) + geom_histogram(alpha=0.4, position="identity")

OK.png

positionの詳細については以下のページの「Position adjustments」のところを見てください。
http://docs.ggplot2.org/current/

参考資料

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14