> 最後にgeom_blankを使い,かつ体裁を最大限整えたグラフを描いてみます。論文にもそのまま使えるほど綺麗なので,参考にしてください。
との触れ込みで,プログラムと図が掲載されていた。
library(dplyr)
library(tidyr)
library(ggplot2)
iris %>%
gather(feature, value, -Species) %>%
group_by(Species, feature) %>%
summarise(mean_value = mean(value)) %>%
group_by(feature) %>%
mutate(y_max = max(mean_value)*1.15) %>%
ggplot(aes(x = Species, y = mean_value, label = mean_value, fill = Species)) +
geom_bar(stat = "identity", width = 0.5, size = 0.3, color = "grey80") +
geom_text(size = 2.4, vjust = -0.5) +
geom_blank(aes(y = y_max)) +
facet_wrap(~feature, ncol = 4, scales = "free") +
scale_y_continuous(expand = c(0, 0)) +
scale_fill_brewer(palette = "Set1") +
theme_bw() +
theme(panel.border = element_blank(),
axis.line = element_line(color = "grey50", size = 0.2),
axis.text.x = element_text(size = 8, angle = 45, hjust = 1, vjust = 1),
axis.text.y = element_text(size = 8),
axis.title = element_text(size = 8),
strip.background = element_blank(),
strip.text = element_text(size = 8, face = "bold"),
axis.ticks = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none") +
labs(x = "Species",
y = "Mean") -> p
なんたる長さ。
この際,平均値を棒グラフで描くことの是非には触れないが,x 軸ラベルも斜めになってジャギー・ジャギー 。
graphics::barplot で書くと
layout(matrix(1:4, 4))
old = par(mar = c(2, 5.5, 2, 1),
mgp = c(1.8, 0.6, 0), las = 1, bty = "l",
tck = -0.05, cex.axis = 1.2)
for (i in 1:4) {
df = iris[, c(i, 5)]
value = by(df[, 1], df$Species, mean)
vname = colnames(df)[1]
pos.bar = barplot(value, bty = "l",
col = c("firebrick3 ", "dodgerblue3 ", "darkgreen "),
xlim = c(0, max(value) * 1.1), horiz = TRUE)
title(main = paste0("mean of ", vname), cex = 0.8)
text(value, pos.bar, value, pos = 2, col = "white", cex = 1.1)
}
par(old)
layout(1)
ぐらいで(まだまだリファインできるけど),次の図が描けた。
こっちの方がやはり,好きだ。