将两个图例添加到绘图中
Add Both Legends to a Plot
我有一个图表,我计算了一段时间内两个样本中的基因数量。我想在瀑布图中绘制两组数字。我能够得到图表,但我很难让两个图例都显示出来。我有下表:
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
是数据框中作为唯一字符的时间列。
谁能帮我在最后的情节中把两个传说放在一起?谢谢。
这里有两种将图例并排放置的方法。
第一个只是添加第一个图例并将它们水平排列。要包含两个图例,请在 scale_fill_manual
中指定每个图例的标题。如果两个图例的标题相同,您可以指定图例的顺序。至少要将图例彼此相邻,将 theme
中的 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")
第二种方法更具实验性。在您的示例中,您的时间图例具有相同的因子(1、7、14、22),因此您可以将图例放在一起。我通过删除第一个图例标签、第二个图例标题并更改 theme
.
中的页边距来实现此目的
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"))
我有一个图表,我计算了一段时间内两个样本中的基因数量。我想在瀑布图中绘制两组数字。我能够得到图表,但我很难让两个图例都显示出来。我有下表:
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
是数据框中作为唯一字符的时间列。
谁能帮我在最后的情节中把两个传说放在一起?谢谢。
这里有两种将图例并排放置的方法。
第一个只是添加第一个图例并将它们水平排列。要包含两个图例,请在 scale_fill_manual
中指定每个图例的标题。如果两个图例的标题相同,您可以指定图例的顺序。至少要将图例彼此相邻,将 theme
中的 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")
第二种方法更具实验性。在您的示例中,您的时间图例具有相同的因子(1、7、14、22),因此您可以将图例放在一起。我通过删除第一个图例标签、第二个图例标题并更改 theme
.
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"))