LoginSignup
4
5

More than 5 years have passed since last update.

「データ視覚化のデザイン #1」をplotで実装する(in R)

Last updated at Posted at 2018-05-31

「データ視覚化のデザイン #1」をmatplotlibで実装するをRのggplot2でやってみるというヤツを書きました

別にggplotを使わずともplotでも書けます。
僕が描くなら版(上記事参照)をplotで描いたやつを供養のため残しておきます。

なんというか、1枚図ならggplotを使わなくてもいいんですよね〜。
ぶっちゃけこっちのほうが見通しが良いと感じるのは、古い人間なのかもしれません。
複数の図をまとめて生成する時は積極的にggplotを使います(facet系)。

tidyverseはデータ処理には必須です。

Fig.1

データは一緒。

dat <- data.frame(name = c("フリーザ", "ギニュー", "クリリン"),
                  val = c(530000, 120000, 10000)) %>% 
  arrange(val)

グラフィックデバイスを起動して、描画して、dev.off()で閉じる。
この場合はpngで.pngを出力するデバイスを起動している。

parのオプションは、R-tipsさんに網羅されているので参照の事。barplotでは軸を書かないでおいて後からaxisで色々と設定すると見通しが良い。

png("fig1_plot.png", width = 700, height = 350, pointsize = 22)

par(family = "HiraKakuPro-W3", tcl = -0.2, mgp = c(2, 0.3, 0), mar = c(3, 5, 1, 1))
barplot(dat$val, horiz = T, axes = F, border = "white", space = 0.1)
axis(2, at = c(1:3)-0.5 + 0.1 * (1:3), labels = dat$name, las = T, pos = 0, col = "white")
axis(1, at = seq(0, 5*10^5, length = 3), 
     labels = seq(0, 5*10^5, length = 3) %>% format(big.mark = ",", scientific = F))
mtext("戦闘力", side = 1, at = 5*10^5, line = 1.5)

dev.off()

Fig.2

データ準備は一緒。色系列作っておく。

dat <- data.frame(year = c(2013:2016),
                  Toyota = c(970, 1010, 1015, 1008),
                  VW = c(975, 1020, 1002, 1035),
                  GM = c(975, 985, 995, 999)) 

cols <- c("mediumseagreen", "indianred2", "dodgerblue1")

plot()type="n"で呼び出して空白を作っておいて、for文で線を追加。
んー図表の外側に文字を書くにはmtextを使います。

png("fig2_plot.png", width = 700, height = 420, pointsize = 22)

par(tcl = -0.3, mgp = c(2, 0.5, 0), mar = c(3, 4, 2, 4))
plot(0, 0, type = "n", axes = F,
     xlim = c(min(dat$year), max(dat$year)), 
     ylim = c(950, 1050),
     xlab = "", ylab = "Auto sales")
for(k in 2:nrow(dat)){
  lines(dat$year, dat[, k], col = cols[k-1], lwd = 5)
}
axis(1, at = 2013:2016, labels = 2013:2016)
axis(2, at = seq(950,1050, length = 3), labels = seq(950,1050, length = 3))
mtext(colnames(dat)[2:4], side = 4, at = dat[4, 2:4], las = T, col = cols)

dev.off()

Fig.3

データは一緒。

library(lubridate)
set.seed(71)

dat <- data.frame(ymd = (ymd("20150501") + months(0:12)),
                  val = seq(450, 990, length = 13) + runif(13, -50, 50)) %>% 
  mutate(time = ymd %>% as.character() %>% str_sub(1, -4))

見たまんま...。
plottype = "o"で呼び出しているぐらいかな。

png("fig3_plot.png", width = 700, height = 560, pointsize = 22)

par(tcl = -0.2, mgp = c(2.5, 0.3, 0), mar = c(1, 4, 0, 2))
plot(dat$val, axes = F, type = "o", pch = 16,
     xlab = "", ylab = "Monthly active users in million",
     ylim = c(0, 1100))
for(k in seq(1, 13, length = 4)){
  lines(c(k, k), c(0, dat$val[k]), lty = 3)
}
axis(1, pos = 0, mgp = c(2.5, 0.4, 0),
     at = seq(1, 13, length = 4), 
     labels = dat$time[seq(1, 13, length = 4)])
axis(2, pos = 0.2)

dev.off()

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