ljh2001
ljh2001

Reputation: 463

Is there a way to combine two similar legends into one simple legend using ggplot?

I've attached two example datasets

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

What I would like to do is get a plot similar to this one, but with a different legend. In the plot above, the two datasets show up as different colors, which is exactly what I want. However, is it possible to remove the blue dots from the legend and instead add a single blue dot above the first black dot that corresponds to a value of 0.0? Alternatively, is there a way to rbind resindna data to the resind2 dataset while achieving the same legend structure?

Upvotes: 1

Views: 35

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174516

This is quite a tricky one, but it is possible. You need to map a dummy variable to the color aesthetic, then make that blue with scale_colour_manual. You then need to change the guide for the size aesthetic to get rid of its blue dots. Finally you need to remove the title from each legend and set the margins to squash them together:

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

enter image description here

Upvotes: 3

Related Questions