第10章の内容

データとパッケージの準備

library(vegan)
library(ggplot2)
Warning: package ‘ggplot2’ was built under R version 4.2.2
phyto_metadata <- readRDS("phyto_metadata.obj")
species_ryuko_data <- readRDS("phyto_ryuko_data.obj")
species_richness <- apply(species_ryuko_data > 0, 1, sum)
total_abundance <- apply(species_ryuko_data, 1, sum)

10.2 Rスクリプトの整理整頓と共有:R Notebook

ウェブページに戻って,R Notebook形式のファイルをダウンロードしよう.

10.3 きれいなグラフを描く:ggplot2

10.3.1 回帰直線と主座標分析を例に

一つ目の例:回帰直線の可視化

####Example 01: 2D scatter plot and regression  line####
#Classical plot (base)
#baseパッケージを使った古典的な作図
model01 <- lm(species_richness ~ phyto_metadata$temp) #単回帰モデルの結果をmodel01に格納
plot(
  species_richness ~ phyto_metadata$temp, 
  type = "p", 
  cex = 3,
  xlab = "temperature",
  ylab = "species richness"
)
abline(model01, col = 4) #回帰直線の重ね合わせ

ggplotで作図するには,応答変数も説明変数も一つのデータフレーム内に格納する必要がある

#ggplotを使ったモダンな作図
#Combine metadata and numeric data into a single dataframe
#メタデータと数値データを一つのデータフレームにまとめる必要がる
lm_forggplot2 <- data.frame(SR = species_richness, TA = total_abundance, phyto_metadata)

シンプルなコードを使った場合の作図

#simple code
ggplot(lm_forggplot2, aes(x = temp, y = SR)) + 
  geom_point(size = 5, aes(shape = as.factor(year)),alpha = 0.5) +
  geom_smooth(method = "lm", se = FALSE, colour = "blue", size = 1) + 
  xlab("temperature") +
  ylab("species richness")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
Please use `linewidth` instead.

もう一つのコードの書き方

#alternative way
lm_ggplot <- ggplot(lm_forggplot2, aes(x = temp, y = SR))
lm_ggplot <- lm_ggplot + geom_point(size = 5, aes(shape = as.factor(year)),alpha = 0.5)
lm_ggplot <- lm_ggplot + geom_smooth(method = "lm", se = FALSE, colour = "blue", size = 1) 
lm_ggplot <- lm_ggplot + xlab("temperature") + ylab("species richness")
print(lm_ggplot)

二つ目の例:主座標分析の可視化

####Exmple 02: PCoA plot####
#Classical plot (base)
#baseパッケージを使った古典的な作図
PCoA_ryuko_BC <- summary(capscale(species_ryuko_data ~ 1, distance = "bray"))
PCoA1_ryuko_BC <- PCoA_ryuko_BC$sites[,1]
PCoA2_ryuko_BC <- PCoA_ryuko_BC$sites[,2]
plot(
  PCoA2_ryuko_BC ~ PCoA1_ryuko_BC,
  cex = 3, pch = as.numeric(as.factor(phyto_metadata$month)),
  xlab = "PCoA1 (25.4 %)", ylab = "PCoA2 (14.7 %) ",
  asp = 1,
  main = "Phyto With Bray-Curtis"
)

#ggplot
#Combine metadata and numeric data of PCoA axes into a single dataframe
#ggplotによるモダンな可視化
#PCoA軸の値をデータフレームに統合する必要がある
ryuko_forggplot2 <- data.frame(lm_forggplot2, PCoA1 = PCoA1_ryuko_BC, PCoA2 = PCoA2_ryuko_BC)

#ggplot2 for 2D scatter plot
g_PCoA <- ggplot(ryuko_forggplot2, aes(x = PCoA1, y = PCoA2))
g_PCoA <- g_PCoA + geom_point(aes(colour = month), size = 8, alpha = 0.5)
g_PCoA <- g_PCoA + labs(x = "PCoA (25.4 %)", y = "PCoA2 (14.7 %)") + ggtitle("Phyto With Bray-Curtis")
print(g_PCoA)

10.3.2 ユニバーサルデザイン

色覚多様性にも配慮した色選択が重要である

#Color should be carefully selected
#可視化に使うデータ列の指定
g_PCoA2 <- ggplot(ryuko_forggplot2, aes(x = PCoA1,y = PCoA2, color = month))
#色の指定
g_PCoA2 <- g_PCoA2 + scale_colour_manual(values = c("#ff4b00", "#4dc4ff", "#f6aa00", "#804000"))
#マークのサイズと形の指定(形はmonthごとに異なる)
g_PCoA2 <- g_PCoA2 + geom_point(size = 8, aes(shape = month), alpha = 1)
#軸ラベルの指定
g_PCoA2 <- g_PCoA2 + labs(x = "PCoA (25.4 %)", y = "PCoA2 (14.7 %)") + ggtitle("Phyto With Bray-Curtis")
#グラフ背景のスタイルの指定
g_PCoA2 <- g_PCoA2 + theme(panel.background = element_rect(fill = "transparent", colour = "black"),panel.grid = element_blank())
print(g_PCoA2)

LS0tDQp0aXRsZTogImdncGxvdDJfZ3JhcGhpY3MuUuOBq+WvvuW/nOOBl+OBn1IgTm90ZWJvb2siDQpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sNCi0tLQ0KDQojIOesrDEw56ug44Gu5YaF5a65DQojIyMg44OH44O844K/44Go44OR44OD44Kx44O844K444Gu5rqW5YKZDQpgYGB7cn0NCmxpYnJhcnkodmVnYW4pDQpsaWJyYXJ5KGdncGxvdDIpDQpwaHl0b19tZXRhZGF0YSA8LSByZWFkUkRTKCJwaHl0b19tZXRhZGF0YS5vYmoiKQ0Kc3BlY2llc19yeXVrb19kYXRhIDwtIHJlYWRSRFMoInBoeXRvX3J5dWtvX2RhdGEub2JqIikNCnNwZWNpZXNfcmljaG5lc3MgPC0gYXBwbHkoc3BlY2llc19yeXVrb19kYXRhID4gMCwgMSwgc3VtKQ0KdG90YWxfYWJ1bmRhbmNlIDwtIGFwcGx5KHNwZWNpZXNfcnl1a29fZGF0YSwgMSwgc3VtKQ0KYGBgDQojIyMgMTAuMiBS44K544Kv44Oq44OX44OI44Gu5pW055CG5pW06aCT44Go5YWx5pyJ77yaUiBOb3RlYm9vaw0K44Km44Kn44OW44Oa44O844K444Gr5oi744Gj44Gm77yMUiBOb3RlYm9va+W9ouW8j+OBruODleOCoeOCpOODq+OCkuODgOOCpuODs+ODreODvOODieOBl+OCiOOBhu+8jg0KDQojIyMgMTAuMyDjgY3jgozjgYTjgarjgrDjg6njg5XjgpLmj4/jgY/vvJpnZ3Bsb3QyIA0KIyMjIyAxMC4zLjEg5Zue5biw55u057ea44Go5Li75bqn5qiZ5YiG5p6Q44KS5L6L44GrDQrkuIDjgaTnm67jga7kvovvvJrlm57luLDnm7Tnt5rjga7lj6/oppbljJYNCmBgYHtyfQ0KIyMjI0V4YW1wbGUgMDE6IDJEIHNjYXR0ZXIgcGxvdCBhbmQgcmVncmVzc2lvbiAgbGluZSMjIyMNC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