Open5

ggplot2のメモ

a2a2

軸ラベルにμをだす

    labs(x=expression(paste("Average Latency [", mu, "s]"))
a2a2

グラフを並べる

グラフを単純に並べるだけでいいなら patchworkを使う

library(patchwork)
g <- g1 + g2 + g3 + plot_layout(ncol = 1)

グループ分けして軸を共通にしたいときはfacet_wrapfacet_gridを使う

p <- ggplot(df, aes(x=x, y=y)) +
    facet_wrap(~region, ncol=4)
a2a2

凡例の順番を指定する

scale_color_manualbreaksで設定する

g <- g +
    scale_color_manual(
        breaks = c("verify", "sign")
    )

[別解] データのソート順を設定する

levels付のfactorに変換する

df$type <- factor(df$type, levels = c("verify", "sign"))
a2a2

凡例をグルーピングする

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))
    )
a2a2

凡例のマーカーを大きくする

geom_point(size = .1)などで書くと凡例がみえにくくなるときの対策

 g <- ggplot() +
    guides(color = guide_legend(override.aes = list(size=1)))