Open5
ggplot2のメモ
軸ラベルにμをだす
labs(x=expression(paste("Average Latency [", mu, "s]"))
グラフを並べる
グラフを単純に並べるだけでいいなら patchwork
を使う
library(patchwork)
g <- g1 + g2 + g3 + plot_layout(ncol = 1)
グループ分けして軸を共通にしたいときはfacet_wrap
かfacet_grid
を使う
p <- ggplot(df, aes(x=x, y=y)) +
facet_wrap(~region, ncol=4)
凡例の順番を指定する
scale_color_manual
の breaks
で設定する
g <- g +
scale_color_manual(
breaks = c("verify", "sign")
)
[別解] データのソート順を設定する
levels
付のfactor
に変換する
df$type <- factor(df$type, levels = c("verify", "sign"))
凡例をグルーピングする
ggnewscale
ライブラリのnew_scale_color()
を使う
library(ggnewscale)
g <- g +
geom_function(...) +
scale_color_manual(
guide = guide_legend(order = 1))
) +
new_scale_color() +
geom_function(...) +
scale_color_manual(
guide = guide_legend(order = 2))
)
凡例のマーカーを大きくする
geom_point(size = .1)などで書くと凡例がみえにくくなるときの対策
g <- ggplot() +
guides(color = guide_legend(override.aes = list(size=1)))