7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rで棒グラフ

Posted at

R(ggplot2)による棒グラフのあれこれです。

ライブラリを読み込み

R
library(ggplot2)
library(dplyr)

基本

geom_colを呼び出します。xおよびyにカラムを指定します。

縦棒グラフ

R
ggplot(BOD, aes(x=Time, y=demand)) +
  geom_col()

image.png

横棒グラフ

R
ggplot(BOD, aes(x=demand, y=Time)) +
  geom_col()

image.png

(参考)連続値とカテゴリによるx軸の違い

上記は、Timeが連続値で、かつ'Time==6'のエントリが存在しないため、該当する棒は歯抜けとなります。
Timeをカテゴリ値に変換することで、'Time==6'の描画対象ではなくなります。

R
ggplot(BOD, aes(x=factor(Time), y=demand)) +
  geom_col()

image.png

色を指定する

枠線と塗りつぶしを指定

R
ggplot(BOD, aes(x=Time, y=demand)) +
  geom_col(fill="lightgreen", colour="black")

image.png

棒のサイズを調整する

R
ggplot(BOD, aes(x=Time, y=demand)) +
  geom_col(width=0.4)

image.png

カラーパレットでグラデーションを指定

R
df <- data.frame(
  name=c("A", "B", "C", "D", "D", "E", "F"),
  value=c(0.87, 0.34, 0.12, -0.19, -0.25, -0.45, -0.79))

ggplot(df, aes(x=value, y=name, fill=value)) +
  geom_col() +
  scale_y_discrete(limits = rev(levels(df$name))) +
  scale_fill_gradient(high="skyblue", low="salmon")

image.png

データの個数を数える棒グラフ

geom_bar()を呼び出します。yには何も指定しません。

R
ggplot(iris, aes(x=Species)) +
  geom_bar()

image.png

データの平均値を示す棒グラフ

stat_summary()を呼び出し、平均値を計算します。

R
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
  stat_summary(fun = mean, geom = "bar")

image.png

変数でグループ化して描画する

  • データ準備
R
df <- data.frame(Titanic) %>%
  group_by(Survived, Class) %>%
  summarize(n=n(), mean=mean(Freq))

それぞれを独立して描く

R
ggplot(df, aes(x=Class, y=mean, fill=Survived)) +
  geom_col(position="dodge")

image.png

積み上げて描く

R
ggplot(df, aes(x=Class, y=mean, fill=Survived)) +
  geom_col(position="stack")

image.png

割合で描く

R
ggplot(df, aes(x=Class, y=mean, fill=Survived)) +
  geom_col(position="fill")

image.png

複数グラフに分けて描く

R
ggplot(df, aes(x=Class, y=mean, fill=Survived)) +
  geom_col() +
  facet_grid(Survived~ .)

image.png

特定の棒だけ異なる色にする

R
df <- mutate(df, focus=ifelse((Class == "Crew") & (Survived == "No"), T, F))

ggplot(df, aes(x=Class, y=mean, group=Survived, fill=focus)) +
  geom_col(position="dodge", colour="white") +
  scale_fill_manual(values=c("grey40", "pink"))

image.png

正負で分岐させた色を塗る

R
df <- mtcars %>%
  scale() %>%
  data.frame() %>%
  mutate(positive=mpg>0)

ggplot(df, aes(x=rownames(mtcars), y=mpg, fill=positive)) +
  geom_col() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) 

image.png

棒ごとのラベルを付加する

R
ggplot(BOD, aes(x=Time, y=demand)) +
  geom_col() + 
  ylim(0,22) + # これを入れないとラベルがグラフ(上)をはみ出してしまう
  geom_text(aes(label=demand), vjust=-1.5)

image.png

7
5
0

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
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?