0
0

More than 1 year has passed since last update.

ggplot2で時系列データの積み上げ棒グラフ:日付軸の書式変更など試す

Last updated at Posted at 2021-09-23

Webのcsvを取得

政府CIOポータル(内閣官房)が提供しているオープンデータを利用します。
今回は「RStudio Cloud」を使用しました。

read.csvを使いcsvファイルを取得します。
colnamesで列名を変更します。

dat <- read.csv("https://vrs-data.cio.go.jp/vaccination/opendata/latest/summary_by_date.csv")

> str(dat)
'data.frame':   164 obs. of  3 variables:
 $ date                        : chr  "2021-04-12" "2021-04-13" "2021-04-14" "2021-04-15" ...
 $ count_first_or_mid_general  : int  4739 3998 5159 8864 10396 7573 9232 33862 51835 62367 ...
 $ count_second_or_full_general: int  0 0 0 0 0 0 0 0 0 0 ...

> head(dat)
        date count_first_or_mid_general
1 2021-04-12                       4739
2 2021-04-13                       3998
3 2021-04-14                       5159
4 2021-04-15                       8864
5 2021-04-16                      10396
6 2021-04-17                       7573
  count_second_or_full_general
1                            0
2                            0
3                            0
4                            0
5                            0
6                            0

> class(dat)
[1] "data.frame"

#列名変更
> colnames(dat) <- c("date","first","second")
> head(dat)
        date first second
1 2021-04-12  4739      0
2 2021-04-13  3998      0
3 2021-04-14  5159      0
4 2021-04-15  8864      0
5 2021-04-16 10396      0
6 2021-04-17  7573      0

パッケージは「RStudio Cloud」から選択・インストールが可能です。

image.png

複数列にまたがる値を1列にまとめる

取得した値は2列に分かれています(接種1回目と2回目)。
積み上げ棒グラフを作成するために、pivot_longerを使い1列にまとめます。

install.packages("tidyverse")
library(dplyr) 
library(tidyr)

#2つの値列を「counts」列にまとめる
#「vaccined」列は列名が入る
dat2 <- dat %>% pivot_longer(-date, names_to = "vaccined", values_to = "counts")

> head(dat2)
# A tibble: 6 × 3
  date       vaccined counts
  <chr>      <chr>     <int>
1 2021-04-12 first      4739
2 2021-04-12 second        0
3 2021-04-13 first      3998
4 2021-04-13 second        0
5 2021-04-14 first      5159
6 2021-04-14 second        0

ggplot2で積み上げ棒グラフ

geom_barを使い積み上げ棒グラフを描きます。

#「fill」で積み上げる値(グループ)を指定
#「identity」で設定したx軸、y軸で棒グラフ描画
g <- ggplot(dat2, aes(x = date, y = counts, fill = vaccined)) +
      geom_bar(stat = "identity")
plot(g)

「RStudio Cloud」上でプレビューが可能です。

image.png

zoom(拡大)もできます。
x軸が読み取れませんがこの後調整します。

image.png

scale_fill_brewerでカラーテーブルを選択して色を変更できます。

g2 <- g + scale_fill_brewer(palette = "Set1")
plot(g2)

image.png

時間軸に変更

strで確認すると、x軸の「date」が「文字列型」になっています。
as.Dateで「日付型」に変更します。

> str(dat2)
tibble [328 × 3] (S3: tbl_df/tbl/data.frame)
 $ date    : chr [1:328] "2021-04-12" "2021-04-12" "2021-04-13" "2021-04-13" ...
 $ vaccined: chr [1:328] "first" "second" "first" "second" ...
 $ counts  : int [1:328] 4739 0 3998 0 5159 0 8864 0 10396 0 ...

dat2$date <- as.Date(dat2$date)
str(dat2)
tibble [328 × 3] (S3: tbl_df/tbl/data.frame)
 $ date    : Date[1:328], format: "2021-04-12" ...
 $ vaccined: chr [1:328] "first" "second" "first" "second" ...
 $ counts  : int [1:328] 4739 0 3998 0 5159 0 8864 0 10396 0 ...

再度積み上げ棒グラフを描画します。

g <- ggplot(dat2, aes(x = date, y = counts, fill = vaccined)) +
      geom_bar(stat = "identity")
plot(g)

x軸が「月」(Month)になりました。

image.png

時間軸を細分化

x軸をもう少し調整します。
scale_x_dateを使い、x軸の目盛り間隔と軸ラベルの表示形式を変更します。

dat3 <- dat2

#「1 week」間隔と指定
#「%m/%d」でmm/ddという表示形式に変更
g3 <- ggplot(dat3, aes(x = date, y = counts, fill = vaccined)) +
  geom_bar(stat = "identity") +
  scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week", date_labels = "%m/%d")
plot(g3)

image.png

x軸ラベルの角度やサイズを変える

もう少しx軸を見やすくするために、axis.text.xを使い、ラベルの角度を調整しました。
また、theme(text = element_text(size = 15))で、図全体のテキストのサイズを大きくします。

#「angle」で角度、「hjust」で縦書き指定
#「theme(text = element_text(size = 15))」でテキストサイズ変更
g3 <- ggplot(dat3, aes(x = date, y = counts, fill = vaccined)) +
  geom_bar(stat = "identity") +
  scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week", date_labels = "%m-%d") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  theme(text = element_text(size = 15))
plot(g3)

併せてx軸のラベルは「mm/dd」から「mm-dd」に変更しています。

image.png

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