2 つのサンプル内の一定期間にわたる遺伝子の数を数えて作成したプロットがあります。両方の数値セットをウォーターフォール グラフにプロットしたいと思います。チャートを取得することはできますが、両方のレジェンドを表示させるのが困難です。次のテーブルがあります:
genotype time degs
1 S 1 dpi 91
2 S 7 dpi 121
3 S 14 dpi 102
4 S 22 dpi 349
9 R 1 dpi 186
10 R 7 dpi 220
11 R 14 dpi 236
12 R 22 dpi 404
genotype time degs
5 S 1 dpi -99
6 S 7 dpi -120
7 S 14 dpi -157
8 S 22 dpi -860
13 R 1 dpi -121
14 R 7 dpi -122
15 R 14 dpi -161
16 R 22 dpi -288
次のコードを使用してプロットを作成しています。
deg.dn$degs=deg.dn$degs*-1
deg.up$time=as.character(deg.up$time)
deg.dn$time=as.character(deg.dn$time)
deg.up$time = factor(deg.up$time, levels=unique(deg.up$time))
deg.dn$time = factor(deg.dn$time, levels=unique(deg.dn$time))
deg.up$genotype=as.character(deg.up$genotype)
deg.dn$genotype=as.character(deg.dn$genotype)
deg.dn$genotype = factor(deg.dn$genotype, levels=unique(deg.dn$genotype))
deg.up$genotype = factor(deg.up$genotype, levels=unique(deg.up$genotype))
breaksup = levels(deg.up$time)
breaksdn = levels(deg.dn$time)
brewup = c("#FEE5D9", "#FCAE91", "#FB6A4A", "#CB181D")
brewdn = c("#EFF3FF", "#BDD7E7", "#6BAED6", "#2171B5")
ggplot() +
geom_bar(data = deg.up, aes(x=genotype, y=degs, fill=time), stat="identity", position="dodge") +
scale_fill_manual(values=brewup, breaks=breaksup) +
new_scale_fill() +
geom_bar(data = deg.dn, aes(x=genotype, y=degs, fill=time), stat="identity", position="dodge") +
scale_fill_manual(values=brewdn, breaks=breaksdn)
breaksup と Breaksdn は、一意の文字としてのデータ フレーム内の時間列です。
最終プロットで両方のレジェンドを隣り合わせにするのを手伝ってくれる人はいますか?ありがとうございます。
------------------------
ここでは、e の横に凡例を配置する 2 つの方法を示します。他にも。
最初の例では、最初の凡例を追加し、それらを水平方向に並べるだけです。両方の凡例を含めるには、scale_fill_manual で各凡例のタイトルを指定します。両方の凡例に同じタイトルが付いている場合は、凡例の順序を指定できます。少なくとも凡例を並べて配置するには、テーマの legend.box を水平に設定します。
ggplot() +
geom_bar(data = deg.up, aes(x=genotype, y=degs, fill=time), stat="identity", position="dodge") +
scale_fill_manual(values=brewup,
breaks=breaksup,
name = "time",
guide = guide_legend(order = 1)) +
new_scale_fill() +
geom_bar(data = deg.dn, aes(x=genotype, y=degs, fill=time), stat="identity", position="dodge") +
scale_fill_manual(values=brewdn,
breaks=breaksdn,
name ="time",
guide = guide_legend(order = 2)) +
theme(legend.box = "horizontal")
2 番目のアプローチはより実験的なものです。あなたの例では、時間の凡例に同じ係数 (1、7、14、22) があるため、凡例を一緒にプッシュする可能性があります。これは、最初の凡例ラベルと 2 番目の凡例タイトルを削除し、テーマの余白を変更することで実現しました。
ggplot() +
geom_bar(data = deg.up, aes(x=genotype, y=degs, fill=time), stat="identity", position="dodge") +
scale_fill_manual(values=brewup, breaks=breaksup,
guide = guide_legend(label = FALSE,
title = "time",
order = 1)) +
new_scale_fill() +
geom_bar(data = deg.dn, aes(x=genotype, y=degs, fill=time), stat="identity", position="dodge") +
scale_fill_manual(values=brewdn,
breaks=breaksdn,
guide = guide_legend(title = "",
order = 2)) +
theme(legend.box = "horizontal",
legend.margin = margin(0,0,0,-.6, "cm"),
legend.box.margin = margin(0,1,0,1, "cm"))
2
ありがとう。これは機能するようですが、theme_bw で試してみると、両方の凡例要素が別々の行に戻ります。 theme_bw に使用すべきコードはありますか?
– パトリック・トーマス
2020 年 9 月 7 日 4:09
theme_bw() は、theme() の上にある必要があります。それを超えると、theme_bw() は凡例の位置をオーバーライドします。 .... + テーマbw() + テーマ(legend.box = "horizontal")
– タムタム
セ2020 年 7 月 6 時 48 分