data.frame(class = names(ab), ab = c(ab)) |>
ggplot(aes(x = class, y = ab, fill = class)) +
geom_col(colour = "black") +
scale_x_discrete(limits = names(ab)) +
scale_fill_manual(values = gray(5:0 / 5)) +
labs(x = "Abundance class",
y = "Frequency") +
theme_classic() +
theme(legend.position = "none")
『Rによる数値生態学』(原著“Numerical Ecology with R, 2nd Edition”)のグラフは基本的にbase graphicsで描かれています。このうち第2章「探索的データ解析」のものを、勉強のためggplot2(とpatchwork)で描いてみようと思いました。
図 2.1
ab
は、各クラスの頻度の値がはいっているtableです。geom_col
を使用します。
図 2.2
サンプリングサイトの位置図です。geom_path
で座標をつなげていきます。
spaは、X座標とY座標のデータフレームですが、rownames
がサイト番号になっていますので、これを新しい列にしておきます。
$Site <- rownames(spa)
spaggplot(spa, aes(x = X, y = Y)) +
geom_path(color = "light blue", linewidth = 1) +
geom_text(aes(label = Site), color = "red", size = 4) +
annotate("text", x = 68, y = 20, label = "Upstream",
size = 5, color = "red") +
annotate("text", x = 15, y = 35, label = "Downstream",
size = 5, color = "red") +
labs(title = "Site locations",
x = "x coordinate (km)",
y = "y coordinate (km)") +
coord_fixed() +
theme_classic()
図 2.3
魚類4種のアバンダンスのバブルマップです。
spe
は、各サイト(行)における各魚種(列)のアバンダンスクラスを格納したデータフレームです。spa
と結合して、pivot_longer
で整然データ(Tidy data)にしています。
<- spa |>
spe_tidy ::bind_cols(spe) |>
dplyr::pivot_longer(cols = Cogo:Anan,
tidyrnames_to = "Species",
values_to = "Abundance")
|>
spe_tidy::filter(Species %in% c("Satr", "Thth", "Baba", "Abbr")) |>
dplyrggplot(aes(x = X, y = Y)) +
geom_path(color = "light blue") +
geom_point(aes(size = Abundance),
color = "brown", shape = 1, na.rm = TRUE) +
scale_size(range = c(0, 6)) +
labs(x = "x coordinate (km)",
y = "y coordinate (km)") +
coord_fixed() +
facet_wrap(~Species, ncol = 2) +
theme_bw(base_size = 9) +
theme(legend.position = "none")
図 2.4
geom_histogram
でヒストグラムを描画します。closed = "left"
で、hist(right = FALSE)
と同じになるようにしています。
patchworkを使って、グラフを並べています。
<- spe_tidy |>
spe_pres ::group_by(Species) |>
dplyr::summarise(pres = sum(Abundance > 0))
dplyr
<- ggplot(spe_pres, aes(x = pres)) +
p1 geom_histogram(breaks = seq(0, 30, by = 5), closed = "left",
fill = "bisque", colour = "black") +
scale_x_continuous(breaks = seq(0, 30, by = 5),
minor_breaks = NULL) +
scale_y_continuous(breaks = seq(0, 12, by = 2),
minor_breaks = NULL) +
labs(title = "Species Occurrence",
x = "Number of occurrence",
y = "Number of species") +
theme_bw(base_size = 9)
<- spe_pres |>
p2 ::mutate(relf = 100 * pres / nrow(spe)) |>
dplyrggplot(aes(x = relf)) +
geom_histogram(breaks = seq(0, 100, by = 10), closed = "left",
fill = "bisque", colour = "black") +
scale_x_continuous(breaks = seq(0, 100, by = 20),
minor_breaks = NULL) +
scale_y_continuous(breaks = seq(0, 7, by = 1),
minor_breaks = NULL) +
labs(title = "Species Relative Frequencies",
x = "Frequency of occurrence (%)",
y = "Number of species") +
theme_bw(base_size = 9)
+ p2 p1
図 2.5
sprch
には、種の豊富さ(出現種数)を入れています。左側のグラフでは、gem_step
を使用しています。
<- spe_tidy |>
sprch ::mutate(Present = (Abundance > 0)) |>
dplyr::group_by(Site) |>
dplyr::summarise(Species_richness = sum(Present))
dplyr
<- ggplot(sprch, aes(x = as.numeric(Site), y = Species_richness)) +
p1 geom_step(color = "gray") +
geom_text(aes(label = Site), colour = "red") +
labs(title = "Species Richness vs. \nUpstream-Downstream Gradient",
x = "Site numbers",
y = "Species richness") +
theme_bw(base_size = 9)
<- dplyr::left_join(spa, sprch, by = "Site") |>
p2 ggplot(aes(x = X, y = Y)) +
geom_point(aes(size = Species_richness), shape = 21,
fill = "brown", colour = "white") +
scale_size(range = c(0, 10)) +
geom_path(color = "light blue", linewidth = 0.5) +
scale_y_continuous(limits = c(10, 120)) +
labs(title = "Map of Species Richness",
x = "x coordinate (km)",
y = "y coordinate (km)") +
coord_fixed() +
theme_bw(base_size = 9) +
theme(legend.position = "none")
+ p2 p1
各種変換した値をspenv_tidy
にまとめます。
<- dplyr::bind_cols(
spenv_tidy |>
spe ::mutate(Site = rownames(spe)) |>
dplyr::pivot_longer(-Site,
tidyrnames_to = "Species",
values_to = "raw"),
|>
spe.pa ::pivot_longer(everything(),
tidyrvalues_to = "pa") |>
::select(pa),
dplyr|>
spe.scal ::pivot_longer(everything(),
tidyrvalues_to = "scal") |>
::select(scal),
dplyr|>
spe.relsp ::pivot_longer(everything(),
tidyrvalues_to = "relsp") |>
::select(relsp),
dplyr|>
spe.rel ::pivot_longer(everything(),
tidyrvalues_to = "rel") |>
::select(rel),
dplyr|>
spe.norm ::pivot_longer(everything(),
tidyrvalues_to = "norm") |>
::select(norm),
dplyr|>
spe.hel ::pivot_longer(everything(),
tidyrvalues_to = "hel") |>
::select(hel),
dplyr|>
spe.chi ::pivot_longer(everything(),
tidyrvalues_to = "chi") |>
::select(chi),
dplyr|>
spe.wis ::pivot_longer(everything(),
tidyrvalues_to = "wis") |>
::select(wis)
dplyr )
図 2.6
箱ひげ図です。これもpatchworkでまとめています。
<- spenv_tidy |>
p1 ::filter(Species == "Babl") |>
dplyr::mutate(raw,
dplyrsqrt = sqrt(raw),
log = log1p(raw),
.keep = "none") |>
::pivot_longer(everything()) |>
tidyr::mutate(name = factor(name,
dplyrlevels = c("raw", "sqrt", "log"))) |>
ggplot(aes(x = name, y = value)) +
geom_boxplot(fill = "bisque") +
labs(title = "Simple transformations", x = "", y = "") +
theme_bw(base_size = 9)
<- spenv_tidy |>
p2 ::filter(Species == "Babl") |>
dplyr::select(scal, relsp) |>
dplyr::pivot_longer(everything()) |>
tidyr::mutate(name = factor(name,
dplyrlevels = c("scal", "relsp"))) |>
ggplot(aes(x = name, y = value)) +
geom_boxplot(fill = "lightgreen") +
scale_x_discrete(labels = c("max", "total")) +
labs(title = "Standardization by species", x = "", y = "") +
theme_bw(base_size = 9)
<- spenv_tidy |>
p3 ::filter(Species == "Babl") |>
dplyr::select(hel, rel, norm) |>
dplyr::pivot_longer(everything()) |>
tidyr::mutate(name = factor(name,
dplyrlevels = c("hel", "rel", "norm"))) |>
ggplot(aes(x = name, y = value)) +
geom_boxplot(fill = "lightblue") +
scale_x_discrete(labels = c("Hellinger", "total", "norm")) +
labs(title = "Standardization by sites", x = "", y = "") +
theme_bw(base_size = 9)
<- spenv_tidy |>
p4 ::filter(Species == "Babl") |>
dplyr::select(chi, wis) |>
dplyr::pivot_longer(everything()) |>
tidyr::mutate(name = factor(name,
dplyrlevels = c("chi", "wis"))) |>
ggplot(aes(x = name, y = value)) +
geom_boxplot(fill = "orange") +
scale_x_discrete(labels = c("Chi-square", "Wisconsin")) +
labs(title = "Double standardizations", x = "", y = "") +
theme_bw(base_size = 9)
+ p2) / (p3 + p4) (p1
p.28-29のグラフ
ここのグラフは本にはコードだけで、図はありません。
描画するグラフにあわせて、データを変形しています。
<- env |>
env_spa ::mutate(Site = rownames(env)) |>
dplyr::left_join(spa, by = "Site")
dplyr
<- c("Satr", "Thth", "Baba", "Abbr", "Babl")
species_level <- c("Brown trout", "Grayling", "Barbel", "Common bream",
species_name "Stone loach")
<- env_spa |>
spenv_dfs ::select(Site, dfs) |>
dplyr::left_join(spenv_tidy, by = "Site")
dplyr
<- scale_colour_manual(labels = species_name,
sc values = c(4, 3, "orange", 2, 1))
<- scale_linetype_manual(labels = species_name,
sl values = c(rep("solid", 4), "dotted"))
<- spenv_dfs |>
p1 ::filter(Species %in% species_level) |>
dplyr::mutate(Species = factor(Species,
dplyrlevels = species_level)) |>
ggplot(aes(x = dfs, y = raw)) +
geom_line(aes(colour = Species, linetype = Species)) +
labs(title = "Raw data",
x = "Distance from the source [km]",
y = "Raw abundance code") +
+ sl +
sc theme_bw(base_size = 9) +
theme(legend.position = "none")
<- spenv_dfs |>
p2 ::filter(Species %in% species_level) |>
dplyr::mutate(Species = factor(Species,
dplyrlevels = species_level)) |>
ggplot(aes(x = dfs, y = scal)) +
geom_line(aes(colour = Species, linetype = Species)) +
labs(title = "Species abundances range by maximum",
x = "Distance from the source [km]",
y = "Ranged abundance") +
+ sl +
sc theme_bw(base_size = 9) +
theme(legend.position = "none")
<- spenv_dfs |>
p3 ::filter(Species %in% species_level) |>
dplyr::mutate(Species = factor(Species,
dplyrlevels = species_level)) |>
ggplot(aes(x = dfs, y = hel)) +
geom_line(aes(colour = Species, linetype = Species)) +
labs(title = "Hellinger-transformed abundances",
x = "Distance from the source [km]",
y = "Standardized abundance") +
+ sl +
sc theme_bw(base_size = 9) +
theme(legend.position = "none")
<- spenv_dfs |>
p4 ::filter(Species %in% species_level) |>
dplyr::mutate(Species = factor(Species,
dplyrlevels = species_level)) |>
ggplot(aes(x = dfs, y = chi)) +
geom_line(aes(colour = Species, linetype = Species)) +
labs(title = "Chi-square-transformed abundances",
x = "Distance from the source [km]",
y = "Standardized abundance") +
+ sl +
sc theme_bw(base_size = 9) +
theme(legend.title = element_text(size = 7),
legend.text = element_text(size = 6),
legend.key.height = unit(7, "points"),
legend.position = "inside",
legend.justification = c(1, 1),
legend.background = element_rect(colour = "black", linewidth = 0.25))
+ p2) / (p3 + p4) (p1
図 2.7
環境条件4変数のバブルマップです。
<- ggplot(env_spa, aes(x = X, y = Y)) +
p1 geom_point(aes(size = 5 * ele / max(ele)),
shape = 21, colour = "white", fill = "red") +
geom_path(colour = "light blue") +
labs(title = "Elevation", x = "x", y = "y") +
coord_fixed() +
theme_bw(base_size = 9) +
theme(legend.position = "none")
<- ggplot(env_spa, aes(x = X, y = Y)) +
p2 geom_point(aes(size = 5 * dis / max(dis)),
shape = 21, colour = "white", fill = "blue") +
geom_path(colour = "light blue") +
labs(title = "Discharge", x = "x", y = "y") +
coord_fixed() +
theme_bw(base_size = 9) +
theme(legend.position = "none")
<- ggplot(env_spa, aes(x = X, y = Y)) +
p3 geom_point(aes(size = 5 * oxy / max(oxy)),
shape = 21, colour = "white", fill = "green3") +
geom_path(colour = "light blue") +
labs(title = "Oxygen", x = "x", y = "y") +
coord_fixed() +
theme_bw(base_size = 9) +
theme(legend.position = "none")
<- ggplot(env_spa, aes(x = X, y = Y)) +
p4 geom_point(aes(size = 5 * nit / max(nit)),
shape = 21, colour = "white", fill = "brown") +
geom_path(colour = "light blue") +
labs(title = "Nitrate", x = "x", y = "y") +
coord_fixed() +
theme_bw(base_size = 9) +
theme(legend.position = "none")
+ p2) / (p3 + p4) (p1
図 2.8
同ラインプロットです。
<- ggplot(env, aes(x = dfs, y = ele)) +
p1 geom_line(colour = "red") +
labs(title = "Elevation",
x = "Distance from the source (km)",
y = "Elevation (m)")+
theme_bw(base_size = 9)
<- ggplot(env, aes(x = dfs, y = dis)) +
p2 geom_line(colour = "blue") +
labs(title = "Discharge",
x = "Distance from the source (km)",
y = expression(paste("Discharge (", m^3, "/s)"))) +
theme_bw(base_size = 9)
<- ggplot(env, aes(x = dfs, y = oxy)) +
p3 geom_line(colour = "green3") +
labs(title = "Oxygen",
x = "Distance from the source (km)",
y = "Oxygen (mg/L)") +
theme_bw(base_size = 9)
<- ggplot(env, aes(x = dfs, y = nit)) +
p4 geom_line(colour = "brown") +
labs(title = "Nitrate",
x = "Distance from the source (km)",
y = "Nitrate (mg/L)") +
theme_bw(base_size = 9)
+ p2) / (p3 + p4) (p1