有没有办法使用 ggplot 将两个相似的图例组合成一个简单的图例?
Is there a way to combine two similar legends into one simple legend using ggplot?
我附上了两个示例数据集
resind2 <- structure(list(Station = structure(c(2L, 2L, 3L, 3L, 4L, 4L,
5L, 5L, 6L, 6L, 26L, 27L, 27L, 28L, 28L, 29L, 29L, 30L, 30L,
31L, 31L, 20L, 21L, 21L, 22L, 22L, 23L, 23L, 24L, 24L, 25L, 25L
), .Label = c("A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
"A8", "B1", "B2", "B3", "B4", "B5", "B6", "C1", "C2", "C3", "C4",
"C5", "C6", "D1", "D2", "E1", "E2", "E3", "E4", "E5", "F1", "F2",
"F3", "F4", "G0", "G1", "G2", "G3", "G4"), class = "factor"),
icegroup = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("pre", "post"
), class = c("ordered", "factor")), mean = c(0.00308641975308642,
0.00308641975308642, 0.00308641975308642, 0.00308641975308642,
0.00308641975308642, NA, 0.00308641975308642, 0.00308641975308642,
0.00925925925925926, NA, 0.0524691358024691, 0.182098765432099,
0.0524691358024691, 0.175925925925926, 0.0493827160493827,
0.160493827160494, 0.095679012345679, 0.160493827160494,
0.0925925925925926, 0.12962962962963, 0.0833333333333333,
0.0294117647058824, 0.0343137254901961, 0.0294117647058824,
0.318627450980392, 0.151960784313726, 0.264705882352941,
0.137254901960784, 0.112745098039216, 0.240196078431373,
0.122549019607843, 0.220588235294118), Lat = c(72.46096,
72.46096, 72.45834, 72.45834, 72.45723, 72.45723, 72.45455,
72.45455, 72.45335, 72.45335, 72.32363, 72.32195, 72.32195,
72.32009, 72.32009, 72.2963, 72.2963, 72.294, 72.294, 72.29368,
72.29368, 72.37331, 72.37086, 72.37086, 72.35019, 72.35019,
72.34705, 72.34705, 72.32738, 72.32738, 72.32544, 72.32544
), Long = c(-80.90488, -80.90488, -80.90544, -80.90544, -80.89737,
-80.89737, -80.89799, -80.89799, -80.89003, -80.89003, -81.14611,
-81.13219, -81.13219, -81.11921, -81.11921, -81.17538, -81.17538,
-81.17191, -81.17191, -81.16389, -81.16389, -81.00075, -80.98878,
-80.98878, -81.09397, -81.09397, -81.08279, -81.08279, -81.17314,
-81.17314, -81.15897, -81.15897), year = c("2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2019", "2019", "2019", "2019", "2019",
"2019", "2019", "2019", "2019", "2019", "2019")), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L, 100L, 101L, 102L, 103L, 104L, 105L,
106L, 107L, 108L, 109L, 110L), class = "data.frame")
resindna <- structure(list(Station = structure(c(4L, 6L, 7L, 8L, 9L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 12L, 13L), .Label = c("A0", "A1",
"A2", "A3", "A4", "A5", "A6", "A7", "A8", "B1", "B2", "B3", "B4",
"B5", "B6", "C1", "C2", "C3", "C4", "C5", "C6", "D1", "D2", "E1",
"E2", "E3", "E4", "E5", "F1", "F2", "F3", "F4", "G0", "G1", "G2",
"G3", "G4"), class = "factor"), icegroup = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L), .Label = c("pre",
"post"), class = c("ordered", "factor")), mean = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Lat = c(72.45723, 72.45335,
72.45078, 72.44962, 72.44698, 72.45834, 72.45723, 72.45455, 72.45335,
72.45078, 72.44962, 72.44698, 72.42462, 72.42056, 72.41856),
Long = c(-80.89737, -80.89003, -80.89063, -80.88254, -80.88313,
-80.90544, -80.89737, -80.89799, -80.89003, -80.89063, -80.88254,
-80.88313, -80.97923, -80.95146, -80.93752), year = c("2018",
"2018", "2018", "2018", "2018", "2019", "2019", "2019", "2019",
"2019", "2019", "2019", "2019", "2019", "2019")), row.names = c(6L,
10L, 12L, 14L, 16L, 71L, 73L, 75L, 77L, 79L, 81L, 83L, 86L, 87L,
89L), class = "data.frame")
ggplot()+
geom_count(data=resind2,aes(x = Long, y = Lat, size = mean), fill = 'white',shape=21)+
geom_point(data = resindna, aes(x = Long, y = Lat), color = 'blue', shape = 21, size = 0.7, show.legend = T)+
facet_grid(year~icegroup, labeller = labeller(icegroup = grouplabs)) + theme_bw()
我想做的是得到一个与此相似的情节,但具有不同的传说。在上图中,两个数据集显示为不同的颜色,这正是我想要的。但是,是否可以从图例中删除蓝点,而是在对应于值 0.0 的第一个黑点上方添加一个蓝点?或者,有没有办法将 resindna 数据 rbind
复制到 resind2 数据集,同时实现相同的图例结构?
这是一个相当棘手的问题,但这是可能的。您需要将虚拟变量映射到颜色美学,然后使用 scale_colour_manual
将其设为蓝色。然后,您需要更改尺寸美学指南以去除其蓝点。最后,您需要从每个图例中删除标题并设置边距以将它们压在一起:
ggplot() +
geom_count(data = resind2,aes(x = Long, y = Lat, size = mean),
fill = 'white', shape = 21, key_glyph = "point")+
geom_point(data = resindna, aes(x = Long, y = Lat, color = "0.0"),
shape = 1, size = 0.7,
show.legend = TRUE) +
scale_color_manual(values = "blue", name = "My title") +
facet_grid(year~icegroup) +
guides(color = guide_legend(order = 1),
size = guide_legend(order = 2, title = NULL,
override.aes = list(size = c(3, 4, 6)))) +
theme_bw() +
theme(legend.margin = margin(-15, 10, 0, 10))
我附上了两个示例数据集
resind2 <- structure(list(Station = structure(c(2L, 2L, 3L, 3L, 4L, 4L,
5L, 5L, 6L, 6L, 26L, 27L, 27L, 28L, 28L, 29L, 29L, 30L, 30L,
31L, 31L, 20L, 21L, 21L, 22L, 22L, 23L, 23L, 24L, 24L, 25L, 25L
), .Label = c("A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
"A8", "B1", "B2", "B3", "B4", "B5", "B6", "C1", "C2", "C3", "C4",
"C5", "C6", "D1", "D2", "E1", "E2", "E3", "E4", "E5", "F1", "F2",
"F3", "F4", "G0", "G1", "G2", "G3", "G4"), class = "factor"),
icegroup = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("pre", "post"
), class = c("ordered", "factor")), mean = c(0.00308641975308642,
0.00308641975308642, 0.00308641975308642, 0.00308641975308642,
0.00308641975308642, NA, 0.00308641975308642, 0.00308641975308642,
0.00925925925925926, NA, 0.0524691358024691, 0.182098765432099,
0.0524691358024691, 0.175925925925926, 0.0493827160493827,
0.160493827160494, 0.095679012345679, 0.160493827160494,
0.0925925925925926, 0.12962962962963, 0.0833333333333333,
0.0294117647058824, 0.0343137254901961, 0.0294117647058824,
0.318627450980392, 0.151960784313726, 0.264705882352941,
0.137254901960784, 0.112745098039216, 0.240196078431373,
0.122549019607843, 0.220588235294118), Lat = c(72.46096,
72.46096, 72.45834, 72.45834, 72.45723, 72.45723, 72.45455,
72.45455, 72.45335, 72.45335, 72.32363, 72.32195, 72.32195,
72.32009, 72.32009, 72.2963, 72.2963, 72.294, 72.294, 72.29368,
72.29368, 72.37331, 72.37086, 72.37086, 72.35019, 72.35019,
72.34705, 72.34705, 72.32738, 72.32738, 72.32544, 72.32544
), Long = c(-80.90488, -80.90488, -80.90544, -80.90544, -80.89737,
-80.89737, -80.89799, -80.89799, -80.89003, -80.89003, -81.14611,
-81.13219, -81.13219, -81.11921, -81.11921, -81.17538, -81.17538,
-81.17191, -81.17191, -81.16389, -81.16389, -81.00075, -80.98878,
-80.98878, -81.09397, -81.09397, -81.08279, -81.08279, -81.17314,
-81.17314, -81.15897, -81.15897), year = c("2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2019", "2019", "2019", "2019", "2019",
"2019", "2019", "2019", "2019", "2019", "2019")), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L, 100L, 101L, 102L, 103L, 104L, 105L,
106L, 107L, 108L, 109L, 110L), class = "data.frame")
resindna <- structure(list(Station = structure(c(4L, 6L, 7L, 8L, 9L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 12L, 13L), .Label = c("A0", "A1",
"A2", "A3", "A4", "A5", "A6", "A7", "A8", "B1", "B2", "B3", "B4",
"B5", "B6", "C1", "C2", "C3", "C4", "C5", "C6", "D1", "D2", "E1",
"E2", "E3", "E4", "E5", "F1", "F2", "F3", "F4", "G0", "G1", "G2",
"G3", "G4"), class = "factor"), icegroup = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L), .Label = c("pre",
"post"), class = c("ordered", "factor")), mean = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Lat = c(72.45723, 72.45335,
72.45078, 72.44962, 72.44698, 72.45834, 72.45723, 72.45455, 72.45335,
72.45078, 72.44962, 72.44698, 72.42462, 72.42056, 72.41856),
Long = c(-80.89737, -80.89003, -80.89063, -80.88254, -80.88313,
-80.90544, -80.89737, -80.89799, -80.89003, -80.89063, -80.88254,
-80.88313, -80.97923, -80.95146, -80.93752), year = c("2018",
"2018", "2018", "2018", "2018", "2019", "2019", "2019", "2019",
"2019", "2019", "2019", "2019", "2019", "2019")), row.names = c(6L,
10L, 12L, 14L, 16L, 71L, 73L, 75L, 77L, 79L, 81L, 83L, 86L, 87L,
89L), class = "data.frame")
ggplot()+
geom_count(data=resind2,aes(x = Long, y = Lat, size = mean), fill = 'white',shape=21)+
geom_point(data = resindna, aes(x = Long, y = Lat), color = 'blue', shape = 21, size = 0.7, show.legend = T)+
facet_grid(year~icegroup, labeller = labeller(icegroup = grouplabs)) + theme_bw()
我想做的是得到一个与此相似的情节,但具有不同的传说。在上图中,两个数据集显示为不同的颜色,这正是我想要的。但是,是否可以从图例中删除蓝点,而是在对应于值 0.0 的第一个黑点上方添加一个蓝点?或者,有没有办法将 resindna 数据 rbind
复制到 resind2 数据集,同时实现相同的图例结构?
这是一个相当棘手的问题,但这是可能的。您需要将虚拟变量映射到颜色美学,然后使用 scale_colour_manual
将其设为蓝色。然后,您需要更改尺寸美学指南以去除其蓝点。最后,您需要从每个图例中删除标题并设置边距以将它们压在一起:
ggplot() +
geom_count(data = resind2,aes(x = Long, y = Lat, size = mean),
fill = 'white', shape = 21, key_glyph = "point")+
geom_point(data = resindna, aes(x = Long, y = Lat, color = "0.0"),
shape = 1, size = 0.7,
show.legend = TRUE) +
scale_color_manual(values = "blue", name = "My title") +
facet_grid(year~icegroup) +
guides(color = guide_legend(order = 1),
size = guide_legend(order = 2, title = NULL,
override.aes = list(size = c(3, 4, 6)))) +
theme_bw() +
theme(legend.margin = margin(-15, 10, 0, 10))