スポンサーリンク

ggplot2で折れ線グラフ(1)

前回の続き

http://twosquirrel.mints.ne.jp/?p=21571

なんか、ggplot2の、stat_summary()関数を重ね合わせようとしても、うまくいかず。

→以下のサイトは、dplyr 関数(?)を使っている(?)

(環境)
Windows 8.1 Pro
R Studio 1.1.383

(1)以下のサイトを写経してみる。

R グラフィックス クックブック 7回目
http://mukkujohn.hatenablog.com/entry/2016/08/22/215632

以下、R Studioで写経して、Knit to HTMLで出力してブラウザで表示した画面をコピペして、

表のところだけ表示がくずれるのでコピペし直した。

ggplot2で折れ線グラフ

R グラフィックス クックブック 7回目

http://mukkujohn.hatenablog.com/entry/2016/08/22/215632

BOD

image

str(BOD)
## 'data.frame':    6 obs. of  2 variables:
##  $ Time  : num  1 2 3 4 5 7
##  $ demand: num  8.3 10.3 19 16 15.6 19.8
##  - attr(*, "reference")= chr "A1.4, p. 270"

x軸にTime列を、y軸にdemand列をマッピングし、折れ線グラフで図示

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.3.3
ggplot(BOD, aes(x=Time, y=demand)) + geom_line()

Time列が、数値型で連続値と捉えれられているため,
Time=6に該当するデータが存在しないのに、図示されている。

離散値とするためには、factor()関数を用いる。

BOD_1 <- BOD
BOD_1$Time <- factor(BOD_1$Time)

str(BOD_1)
## 'data.frame':    6 obs. of  2 variables:
##  $ Time  : Factor w/ 6 levels "1","2","3","4",..: 1 2 3 4 5 6
##  $ demand: num  8.3 10.3 19 16 15.6 19.8
##  - attr(*, "reference")= chr "A1.4, p. 270"
library(ggplot2)
ggplot(BOD_1, aes(x=Time, y=demand, group=1)) + geom_line()

y軸の表示範囲を、0始まりにしたい場合

  • ylim()で範囲を指定する
  • expand_limits()で任意の値を含む様に指定する
bodLineChart <-
  ggplot(BOD_1, aes(x=Time, y=demand, group=1)) +
  geom_line()
plot(bodLineChart)

## ylim()で範囲を指定する

bodLineChart <-
  ggplot(BOD_1, aes(x=Time, y=demand, group=1)) +
  geom_line()
bodLineChart <- bodLineChart + ylim(0, max(BOD_1$demand))
plot(bodLineChart)

## expand_limits()で任意の値を含む様に指定する

bodLineChart <-
  ggplot(BOD_1, aes(x=Time, y=demand, group=1)) +
  geom_line()
bodLineChart <- bodLineChart + expand_limits(y=0)
plot(bodLineChart)

点をもつ折れ線グラフ

BOD_1 <- BOD
BOD_1$Time <- factor(BOD_1$Time)

str(BOD_1)
## 'data.frame':    6 obs. of  2 variables:
##  $ Time  : Factor w/ 6 levels "1","2","3","4",..: 1 2 3 4 5 6
##  $ demand: num  8.3 10.3 19 16 15.6 19.8
##  - attr(*, "reference")= chr "A1.4, p. 270"
library(ggplot2)
ggplot(BOD_1, aes(x=Time, y=demand, group=1)) +
  geom_line() +
  geom_point()

# 複数の線の折れ線グラフ

head(ToothGrowth)

image

str(ToothGrowth)
## 'data.frame':    60 obs. of  3 variables:
##  $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
##  $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
##  $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...

dose列をx軸にとり、supp列の値でグループ化された、lenの平均値を折れ線グラフに図示する

データの加工にplyrパッケージを利用

library(plyr)
## Warning: package 'plyr' was built under R version 3.3.3
tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length=mean(len))

tg

 

str(tg)
## 'data.frame':    6 obs. of  3 variables:
##  $ supp  : Factor w/ 2 levels "OJ","VC": 1 1 1 2 2 2
##  $ dose  : num  0.5 1 2 0.5 1 2
##  $ length: num  13.23 22.7 26.06 7.98 16.77 ...

複数の線を図示するには、colourかlinetypeに離散値のグループを指定

library(plyr)
tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length=mean(len))

library(ggplot2)
ggplot(tg, aes(x=dose, y=length, colour=supp)) + geom_line()

linetypeに指定した場合

library(plyr)
tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length=mean(len))

library(ggplot2)
ggplot(tg, aes(x=dose, y=length, linetype=supp)) + geom_line()

dose列が数値型であり、連続値として扱われているため、データに存在しない1.5がグラフに表れている。 そのため、ファクタに変換し、離散値として扱う。

library(plyr)
tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length=mean(len))

library(ggplot2)
ggplot(tg, aes(x=factor(dose), y=length, colour=supp, group=supp)) +
  geom_line()

 

R Studioの画面は以下のような感じ。

image

 

R Studioと、R Notebookの組み合わせ、かなり便利かもしれない。

 

次は、以下のサイトを写経してみたい。

2015年 08月 20日
ggplot2を使ったグラフ作成(折れ線、時系列)
http://datator.exblog.jp/24805889/

http://datator.exblog.jp/24809125/

スポンサーリンク

R

Posted by twosquirrel