1

I am trying to combine two legends in R, so they show up closer together. This is different to the usual query people ask on combining legends, which involves combining legends so that, say, a shape and a colour are combined in to one legend.

I basically want exactly what can be seen in the attached image, but with the 3 legend items being grouped together; so that the line one is directly below the bar one. My first time using R (just doing it to practice before a course), so feel free to criticise the rest of my code; but I'm happy enough with how it looks so far apart from these two parts of the legend not being closer to each other. I have searched a lot for a solution. I can add a fill to the line aesthetics, which shows up with the other bar items in the legend, but that then gives me a block on the key rather than the line I want.

       geom_col(data = subset(surrey_total,date >= MaxDate - 30),aes(fill=TheColumnColours)) +
       geom_line(key_glyph = "abline",data=subset(surrey_total, date >= MaxDate - 35),aes(color="7-day average",y=rollmean(newCasesBySpecimenDate, 7, fill = NA)), size=1) +
       geom_text(color=TheDigitColours,data = subset(surrey_total,date >= MaxDate - 30),size=3,aes(fontface=TheDigitBold,label = newCasesBySpecimenDate), position = position_dodge(0.9), vjust = 1.5) +
       scale_x_date(breaks = TheBreaks, labels = date_format("%d-%b"), limits=c(MaxDate-31, MaxDate+1), expand = c(0, 0)) +
       scale_y_continuous(breaks=seq(0,4000,by = 200), minor_breaks=NULL,expand = c(0, 0)) +
       ylab("Number of Cases") + xlab("Date") + ggtitle("Surrey Daily Cases") + theme(plot.title = element_text(hjust = 0.5)) +
       labs(color= NA,fill = "Legend", subtitle = paste(format(surrey_rate_table[surrey_rate_table$areaName=="Surrey Total",]$newCasesBySpecimenDate, big.mark=","), " cases in last 7 days - 7 day rate ", surrey_rate_table[surrey_rate_table$areaName=="Surrey Total",]$newCasesBySpecimenDateRollingRate,"/100,000",sep="")) +
       theme(legend.key = element_rect(fill = NA, color = NA), legend.position = c(0.06,0.85),legend.background = element_rect(fill = NA, color = NA), legend.title = element_blank(), axis.line = element_line(color = 'black'),panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), panel.background = element_rect(fill = "white"),
       panel.grid.major.y = element_line(color="gray", linetype="dotted"),panel.border = element_blank())+#,text=element_text(family="Verdana")) +
       scale_color_manual(name = "Legend",values = ColourSelections) +
       scale_fill_manual(name = "Legend",values = ColourSelections) 

https://i.sstatic.net/27Fz1.png

As you can see, I've added two scales, one for the colour (of the line) and one for the fill (of the bars), and I think this means they show up separately. I had read that giving them the same name would mean they group together but this doesn't appear to work.

surrey_df:

18592, 18593, 18594, 18595, 18596, 18597, 18598, 18599, 18600, 
18601, 18602, 18603, 18604, 18605, 18606, 18607, 18608, 18609, 
18610, 18611, 18612, 18613, 18614, 18615, 18616, 18617, 18618, 
18619, 18620, 18621, 18622, 18623, 18624, 18625, 18626, 18627, 
18628, 18588, 18589, 18590, 18591, 18592, 18593, 18594, 18595, 
18596, 18597, 18598, 18599, 18600, 18601, 18602, 18603, 18604, 
18605, 18606, 18607, 18608, 18609, 18610, 18611, 18612, 18613, 
18614, 18615, 18616, 18617, 18618, 18619, 18620, 18621, 18622, 
18623, 18624, 18625, 18626, 18627, 18628, 18588, 18589, 18590, 
18591, 18592, 18593, 18594, 18595, 18596, 18597, 18598, 18599, 
18600, 18601, 18602, 18603, 18604, 18605, 18606, 18607, 18608, 
18609, 18610, 18611, 18612, 18613, 18614, 18615, 18616, 18617, 
18618, 18619, 18620, 18621, 18622, 18623, 18624, 18625, 18626, 
18627, 18628, 18588, 18589, 18590, 18591, 18592, 18593, 18594, 
18595, 18596, 18597, 18598, 18599, 18600, 18601, 18602, 18603, 
18604, 18605, 18606, 18607, 18608, 18609, 18610, 18611, 18612, 
18613, 18614, 18615, 18616, 18617, 18618, 18619, 18620, 18621, 
18622, 18623, 18624, 18625, 18626, 18627, 18628, 18588, 18589, 
18590, 18591, 18592, 18593, 18594, 18595, 18596, 18597, 18598, 
18599, 18600, 18601, 18602, 18603, 18604, 18605, 18606, 18607, 
18608, 18609, 18610, 18611, 18612, 18613, 18614, 18615, 18616, 
18617, 18618, 18619, 18620, 18621, 18622, 18623, 18624, 18625, 
18626, 18627, 18628, 18588, 18589, 18590, 18591, 18592, 18593, 
18594, 18595, 18596, 18597, 18598, 18599, 18600, 18601, 18602, 
18603, 18604, 18605, 18606, 18607, 18608, 18609, 18610, 18611, 
18612, 18613, 18614, 18615, 18616, 18617, 18618, 18619, 18620, 
18621, 18622, 18623, 18624, 18625, 18626, 18627, 18628, 18588, 
18589, 18590, 18591, 18592, 18593, 18594, 18595, 18596, 18597, 
18598, 18599, 18600, 18601, 18602, 18603, 18604, 18605, 18606, 
18607, 18608, 18609, 18610, 18611, 18612, 18613, 18614, 18615, 
18616, 18617, 18618, 18619, 18620, 18621, 18622, 18623, 18624, 
18625, 18626, 18627, 18628, 18588, 18589, 18590, 18591, 18592, 
18593, 18594, 18595, 18596, 18597, 18598, 18599, 18600, 18601, 
18602, 18603, 18604, 18605, 18606, 18607, 18608, 18609, 18610, 
18611, 18612, 18613, 18614, 18615, 18616, 18617, 18618, 18619, 
18620, 18621, 18622, 18623, 18624, 18625, 18626, 18627, 18628, 
18588, 18589, 18590, 18591, 18592, 18593, 18594, 18595, 18596, 
18597, 18598, 18599, 18600, 18601, 18602, 18603, 18604, 18605, 
18606, 18607, 18608, 18609, 18610, 18611, 18612, 18613, 18614, 
18615, 18616, 18617, 18618, 18619, 18620, 18621, 18622, 18623, 
18624, 18625, 18626, 18627, 18628, 18588, 18589, 18590, 18591, 
18592, 18593, 18594, 18595, 18596, 18597, 18598, 18599, 18600, 
18601, 18602, 18603, 18604, 18605, 18606, 18607, 18608, 18609, 
18610, 18611, 18612, 18613, 18614, 18615, 18616, 18617, 18618, 
18619, 18620, 18621, 18622, 18623, 18624, 18625, 18626, 18627, 
18628, 18588, 18589, 18590, 18591, 18592, 18593, 18594, 18595, 
18596, 18597, 18598, 18599, 18600, 18601, 18602, 18603, 18604, 
18605, 18606, 18607, 18608, 18609, 18610, 18611, 18612, 18613, 
18614, 18615, 18616, 18617, 18618, 18619, 18620, 18621, 18622, 
18623, 18624, 18625, 18626, 18627, 18628), class = "Date"), areaName = c("Elmbridge", 
"Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", 
"Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", 
"Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", 
"Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", 
"Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", 
"Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", 
"Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", 
"Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", "Elmbridge", 
"Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", 
"Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", 
"Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", 
"Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", 
"Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", 
"Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", 
"Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", 
"Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", 
"Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", 
"Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", "Epsom and Ewell", 
"Epsom and Ewell", "Guildford", "Guildford", "Guildford", "Guildford", 
"Guildford", "Guildford", "Guildford", "Guildford", "Guildford", 
"Guildford", "Guildford", "Guildford", "Guildford", "Guildford", 
"Guildford", "Guildford", "Guildford", "Guildford", "Guildford", 
"Guildford", "Guildford", "Guildford", "Guildford", "Guildford", 
"Guildford", "Guildford", "Guildford", "Guildford", "Guildford", 
"Guildford", "Guildford", "Guildford", "Guildford", "Guildford", 
"Guildford", "Guildford", "Guildford", "Guildford", "Guildford", 
"Guildford", "Guildford", "Mole Valley", "Mole Valley", "Mole Valley", 
"Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", 
"Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", 
"Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", 
"Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", 
"Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", 
"Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", 
"Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", "Mole Valley", 
"Mole Valley", "Mole Valley", "Mole Valley", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Reigate and Banstead", "Reigate and Banstead", 
"Reigate and Banstead", "Runnymede", "Runnymede", "Runnymede", 
"Runnymede", "Runnymede", "Runnymede", "Runnymede", "Runnymede", 
"Runnymede", "Runnymede", "Runnymede", "Runnymede", "Runnymede", 
"Runnymede", "Runnymede", "Runnymede", "Runnymede", "Runnymede", 
"Runnymede", "Runnymede", "Runnymede", "Runnymede", "Runnymede", 
"Runnymede", "Runnymede", "Runnymede", "Runnymede", "Runnymede", 
"Runnymede", "Runnymede", "Runnymede", "Runnymede", "Runnymede", 
"Runnymede", "Runnymede", "Runnymede", "Runnymede", "Runnymede", 
"Runnymede", "Runnymede", "Runnymede", "Spelthorne", "Spelthorne", 
"Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", 
"Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", 
"Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", 
"Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", 
"Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", 
"Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", 
"Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", 
"Spelthorne", "Spelthorne", "Spelthorne", "Spelthorne", "Surrey Heath", 
"Surrey Heath", "Surrey Heath", "Surrey Heath", "Surrey Heath", 
"Surrey Heath", "Surrey Heath", "Surrey Heath", "Surrey Heath", 
"Surrey Heath", "Surrey Heath", "Surrey Heath", "Surrey Heath", 
"Surrey Heath", "Surrey Heath", "Surrey Heath", "Surrey Heath", 
"Surrey Heath", "Surrey Heath", "Surrey Heath", "Surrey Heath", 
"Surrey Heath", "Surrey Heath", "Surrey Heath", "Surrey Heath", 
"Surrey Heath", "Surrey Heath", "Surrey Heath", "Surrey Heath", 
"Surrey Heath", "Surrey Heath", "Surrey Heath", "Surrey Heath", 
"Surrey Heath", "Surrey Heath", "Surrey Heath", "Surrey Heath", 
"Surrey Heath", "Surrey Heath", "Surrey Heath", "Surrey Heath", 
"Tandridge", "Tandridge", "Tandridge", "Tandridge", "Tandridge", 
"Tandridge", "Tandridge", "Tandridge", "Tandridge", "Tandridge", 
"Tandridge", "Tandridge", "Tandridge", "Tandridge", "Tandridge", 
"Tandridge", "Tandridge", "Tandridge", "Tandridge", "Tandridge", 
"Tandridge", "Tandridge", "Tandridge", "Tandridge", "Tandridge", 
"Tandridge", "Tandridge", "Tandridge", "Tandridge", "Tandridge", 
"Tandridge", "Tandridge", "Tandridge", "Tandridge", "Tandridge", 
"Tandridge", "Tandridge", "Tandridge", "Tandridge", "Tandridge", 
"Tandridge", "Waverley", "Waverley", "Waverley", "Waverley", 
"Waverley", "Waverley", "Waverley", "Waverley", "Waverley", "Waverley", 
"Waverley", "Waverley", "Waverley", "Waverley", "Waverley", "Waverley", 
"Waverley", "Waverley", "Waverley", "Waverley", "Waverley", "Waverley", 
"Waverley", "Waverley", "Waverley", "Waverley", "Waverley", "Waverley", 
"Waverley", "Waverley", "Waverley", "Waverley", "Waverley", "Waverley", 
"Waverley", "Waverley", "Waverley", "Waverley", "Waverley", "Waverley", 
"Waverley", "Woking", "Woking", "Woking", "Woking", "Woking", 
"Woking", "Woking", "Woking", "Woking", "Woking", "Woking", "Woking", 
"Woking", "Woking", "Woking", "Woking", "Woking", "Woking", "Woking", 
"Woking", "Woking", "Woking", "Woking", "Woking", "Woking", "Woking", 
"Woking", "Woking", "Woking", "Woking", "Woking", "Woking", "Woking", 
"Woking", "Woking", "Woking", "Woking", "Woking", "Woking", "Woking", 
"Woking"), newCasesBySpecimenDate = c(13, 15, 20, 20, 23, 19, 
12, 17, 22, 28, 31, 22, 22, 14, 20, 33, 32, 44, 46, 35, 47, 44, 
61, 61, 89, 80, 93, 74, 133, 168, 145, 114, 138, 39, 116, 132, 
121, 228, 208, 147, 101, 9, 14, 11, 17, 11, 16, 12, 13, 35, 21, 
9, 15, 14, 9, 10, 38, 31, 28, 23, 50, 30, 34, 36, 73, 58, 67, 
58, 39, 59, 100, 103, 104, 67, 33, 101, 89, 58, 153, 128, 97, 
48, 15, 33, 25, 25, 17, 27, 20, 15, 24, 26, 20, 27, 16, 26, 19, 
26, 40, 39, 43, 49, 38, 45, 67, 58, 89, 51, 78, 39, 74, 120, 
151, 84, 110, 42, 140, 126, 97, 172, 140, 111, 81, 7, 15, 19, 
13, 15, 16, 15, 18, 19, 11, 8, 10, 6, 12, 15, 8, 13, 25, 30, 
28, 18, 31, 34, 35, 43, 37, 55, 29, 37, 72, 64, 82, 61, 11, 77, 
64, 66, 96, 65, 71, 49, 8, 18, 26, 20, 21, 21, 12, 12, 38, 22, 
25, 23, 22, 26, 15, 22, 36, 28, 46, 52, 55, 64, 78, 105, 104, 
78, 91, 51, 110, 169, 175, 111, 120, 40, 144, 195, 142, 247, 
215, 144, 140, 15, 21, 19, 13, 25, 28, 23, 38, 29, 36, 49, 49, 
40, 20, 15, 33, 25, 30, 29, 31, 32, 39, 48, 75, 56, 54, 49, 36, 
51, 79, 72, 70, 57, 26, 76, 88, 56, 98, 131, 83, 51, 17, 20, 
19, 18, 19, 11, 10, 27, 18, 28, 23, 26, 23, 28, 25, 42, 35, 24, 
40, 53, 25, 53, 48, 105, 87, 71, 82, 43, 114, 115, 140, 93, 103, 
31, 106, 118, 146, 198, 139, 100, 82, 25, 18, 15, 16, 17, 14, 
13, 16, 24, 24, 9, 18, 20, 19, 18, 24, 22, 29, 45, 46, 54, 36, 
57, 48, 62, 58, 57, 41, 53, 101, 90, 55, 75, 24, 87, 99, 79, 
151, 145, 93, 68, 8, 13, 14, 8, 7, 22, 11, 6, 29, 22, 19, 22, 
30, 15, 23, 29, 23, 37, 29, 44, 37, 38, 72, 70, 67, 61, 77, 49, 
75, 123, 127, 78, 78, 21, 98, 98, 89, 153, 131, 71, 85, 14, 17, 
18, 12, 6, 14, 4, 7, 21, 15, 16, 9, 19, 10, 17, 20, 20, 18, 46, 
36, 24, 27, 48, 37, 56, 40, 74, 38, 54, 77, 101, 76, 55, 18, 
95, 114, 104, 188, 164, 91, 99, 13, 44, 30, 25, 31, 25, 27, 19, 
31, 48, 41, 27, 32, 27, 35, 38, 45, 42, 47, 48, 36, 46, 70, 63, 
82, 93, 86, 57, 77, 104, 105, 75, 83, 22, 87, 132, 92, 161, 112, 
90, 72), newDeaths28DaysByDeathDate = c(1, 1, 0, 3, 0, 0, 0, 
0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 
0, 2, 0, 0, 0, 0, 2, 2, 2, 3, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 
3, 0, 0, 0, 1, 2, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 3, 0, 
1, 0, 2, 1, 1, 0, 0, 1, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 2, 0, 
0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 2, 
0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 0, 0, 1, 1, 2, 1, 2, 
3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 0, 1, 0, 0, 1, 0, 2, 0, 
0, 1, 1, 1, 3, 1, 0, 0, 0, 2, 1, 1, 2, 0, 0, 2, 3, 3, 0, 2, 2, 
3, 1, 0, 2, 0, 0, 3, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 3, 0, 1, 
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 
0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 2, 2, 
3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 
2, 0, 2, 0, 2, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
0, 2, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 2, 1, 1, 1, 1, 
0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 
1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 4, 2, 0, 2, 3, 0, 0, 2, 1, 0, 2, 
2, 1, 1, 1, 0, 0, 2, 0, 1, 2, 2, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 
1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 2, 1, 1, 0, 0, 0, 0, 
0, 0, 0), newCasesBySpecimenDateRollingRate = c(106, 95, 86.3, 
79, 87, 89.9, 89.2, 92.1, 97.2, 103.1, 111.1, 110.4, 112.6, 114, 
116.2, 124.3, 127.2, 136.7, 154.2, 163.7, 187.9, 205.4, 225.9, 
247.1, 280, 304.8, 347.2, 367, 432, 510.3, 571.7, 589.9, 632.3, 
592.9, 623.6, 622.8, 588.5, 649.1, 717.9, 724.4, 0, 165, 136.4, 
125.3, 127.7, 119.1, 110.4, 111.6, 116.6, 142.6, 155, 145.1, 
150.1, 147.6, 143.9, 140.2, 143.9, 156.3, 179.8, 189.8, 234.4, 
260.5, 290.2, 287.7, 339.8, 377, 431.6, 441.5, 452.7, 483.7, 
563.1, 600.3, 657.3, 657.3, 626.3, 703.2, 740.4, 688.4, 750.4, 
780.1, 817.3, 0, 120.1, 112.1, 116.8, 112.8, 113.4, 109.4, 108.7, 
108.7, 102.7, 103.4, 100, 106.7, 99.3, 103.4, 106, 107.4, 116.8, 
129.5, 140.3, 162.4, 170.5, 187.9, 215.4, 227.5, 261.1, 266.4, 
285.9, 286.6, 306, 341.6, 404, 400.7, 440.3, 416.1, 483.9, 518.8, 
503.4, 517.5, 555, 555.7, 0, 97.4, 89.4, 92.8, 98.6, 100.9, 108.9, 
114.6, 127.2, 131.8, 122.6, 116.9, 111.2, 99.7, 96.3, 92.8, 80.2, 
82.5, 102, 124.9, 150.2, 157, 175.4, 205.2, 230.4, 251, 259, 
290, 302.6, 309.5, 353, 386.3, 431, 458.5, 408, 463.1, 494, 487.1, 
523.8, 504.3, 515.8, 0, 103.5, 90.8, 93.4, 94.8, 94.1, 89.4, 
84.7, 87.4, 100.8, 98.2, 101.5, 102.9, 103.5, 112.9, 115, 104.2, 
113.6, 115.6, 131.1, 151.3, 170.8, 203.7, 241.3, 287.7, 338.8, 
360.3, 386.6, 383.9, 414.8, 476, 523, 527.7, 556, 521.7, 584.2, 
641.4, 623.2, 671.6, 741.5, 757.7, 0, 199.1, 185.6, 153.2, 145.4, 
144.3, 156.6, 161, 186.8, 195.7, 214.7, 255, 281.8, 295.2, 291.9, 
266.1, 270.6, 258.3, 237.1, 214.7, 204.6, 218.1, 244.9, 261.7, 
317.6, 346.7, 374.6, 394.7, 399.2, 412.6, 447.3, 444, 459.6, 
463, 437.2, 482, 523.3, 497.6, 526.7, 594.9, 624, 0, 192.3, 171.3, 
150.2, 140.2, 125.2, 114.2, 114.2, 124.2, 122.2, 131.2, 136.2, 
143.2, 155.2, 173.3, 171.3, 195.3, 202.3, 203.3, 217.3, 247.4, 
244.4, 272.4, 278.4, 348.5, 411.6, 442.7, 471.7, 489.8, 550.9, 
618, 653, 659, 691.1, 640, 703.1, 707.1, 738.2, 796.2, 842.3, 
839.3, 0, 153.4, 156.8, 146.7, 141.1, 138.9, 136.6, 132.1, 122.1, 
128.8, 138.9, 131, 132.1, 138.9, 145.6, 147.8, 147.8, 145.6, 
168, 198.2, 227.3, 266.5, 286.7, 323.6, 352.7, 389.7, 404.2, 
416.6, 402, 421, 470.3, 517.3, 509.5, 528.5, 491.6, 543.1, 594.6, 
570, 638.3, 739, 759.2, 0, 112.3, 97.6, 87.4, 81.7, 81.7, 91.9, 
94.2, 91.9, 110.1, 119.1, 131.6, 148.6, 157.7, 162.3, 181.6, 
181.6, 182.7, 203.1, 211.1, 226.9, 251.9, 268.9, 317.7, 371, 
405.1, 441.4, 478.8, 492.5, 534.4, 592.3, 657, 669.5, 688.8, 
625.2, 680.8, 706.9, 668.3, 697.8, 758, 750, 0, 84.7, 88.7, 82.3, 
76, 76, 74.4, 67.3, 61.7, 64.9, 62.5, 65.7, 68.1, 72, 76.8, 84.7, 
83.9, 87.9, 89.4, 118.7, 132.2, 143.3, 151.2, 173.4, 186.8, 216.9, 
212.1, 242.2, 253.3, 274.7, 297.6, 348.3, 364.1, 376, 331.7, 
376.8, 424.3, 445.7, 514.5, 584.2, 612.7, 0, 145.8, 173.6, 181.6, 
174.6, 187.5, 198.4, 193.5, 199.4, 186.5, 204.4, 220.3, 216.3, 
223.2, 223.2, 239.1, 246, 243.1, 244.1, 263.9, 279.8, 288.7, 
299.6, 331.4, 349.2, 388.9, 434.6, 472.3, 493.1, 523.8, 557.6, 
599.2, 592.3, 582.4, 518.9, 548.6, 603.2, 591.3, 646.9, 683.6, 
690.5, 0), Month = structure(c(18567, 18567, 18567, 18567, 18567, 
18567, 18567, 18567, 18567, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18628, 
18567, 18567, 18567, 18567, 18567, 18567, 18567, 18567, 18567, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18628, 18567, 18567, 18567, 18567, 
18567, 18567, 18567, 18567, 18567, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18628, 18567, 18567, 18567, 18567, 18567, 18567, 18567, 18567, 
18567, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18628, 18567, 18567, 18567, 
18567, 18567, 18567, 18567, 18567, 18567, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18628, 18567, 18567, 18567, 18567, 18567, 18567, 18567, 
18567, 18567, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18628, 18567, 18567, 
18567, 18567, 18567, 18567, 18567, 18567, 18567, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18628, 18567, 18567, 18567, 18567, 18567, 18567, 
18567, 18567, 18567, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18628, 18567, 
18567, 18567, 18567, 18567, 18567, 18567, 18567, 18567, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18628, 18567, 18567, 18567, 18567, 18567, 
18567, 18567, 18567, 18567, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18628, 
18567, 18567, 18567, 18567, 18567, 18567, 18567, 18567, 18567, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 18597, 
18597, 18597, 18597, 18597, 18628), class = "Date")), row.names = c(325L, 
326L, 327L, 328L, 329L, 330L, 331L, 332L, 333L, 334L, 335L, 336L, 
337L, 338L, 339L, 340L, 341L, 342L, 343L, 344L, 345L, 346L, 347L, 
348L, 349L, 350L, 351L, 352L, 353L, 354L, 355L, 356L, 357L, 358L, 
359L, 360L, 361L, 362L, 363L, 364L, 365L, 693L, 694L, 695L, 696L, 
697L, 698L, 699L, 700L, 701L, 702L, 703L, 704L, 705L, 706L, 707L, 
708L, 709L, 710L, 711L, 712L, 713L, 714L, 715L, 716L, 717L, 718L, 
719L, 720L, 721L, 722L, 723L, 724L, 725L, 726L, 727L, 728L, 729L, 
730L, 731L, 732L, 733L, 1061L, 1062L, 1063L, 1064L, 1065L, 1066L, 
1067L, 1068L, 1069L, 1070L, 1071L, 1072L, 1073L, 1074L, 1075L, 
1076L, 1077L, 1078L, 1079L, 1080L, 1081L, 1082L, 1083L, 1084L, 
1085L, 1086L, 1087L, 1088L, 1089L, 1090L, 1091L, 1092L, 1093L, 
1094L, 1095L, 1096L, 1097L, 1098L, 1099L, 1100L, 1101L, 1429L, 
1430L, 1431L, 1432L, 1433L, 1434L, 1435L, 1436L, 1437L, 1438L, 
1439L, 1440L, 1441L, 1442L, 1443L, 1444L, 1445L, 1446L, 1447L, 
1448L, 1449L, 1450L, 1451L, 1452L, 1453L, 1454L, 1455L, 1456L, 
1457L, 1458L, 1459L, 1460L, 1461L, 1462L, 1463L, 1464L, 1465L, 
1466L, 1467L, 1468L, 1469L, 1797L, 1798L, 1799L, 1800L, 1801L, 
1802L, 1803L, 1804L, 1805L, 1806L, 1807L, 1808L, 1809L, 1810L, 
1811L, 1812L, 1813L, 1814L, 1815L, 1816L, 1817L, 1818L, 1819L, 
1820L, 1821L, 1822L, 1823L, 1824L, 1825L, 1826L, 1827L, 1828L, 
1829L, 1830L, 1831L, 1832L, 1833L, 1834L, 1835L, 1836L, 1837L, 
2165L, 2166L, 2167L, 2168L, 2169L, 2170L, 2171L, 2172L, 2173L, 
2174L, 2175L, 2176L, 2177L, 2178L, 2179L, 2180L, 2181L, 2182L, 
2183L, 2184L, 2185L, 2186L, 2187L, 2188L, 2189L, 2190L, 2191L, 
2192L, 2193L, 2194L, 2195L, 2196L, 2197L, 2198L, 2199L, 2200L, 
2201L, 2202L, 2203L, 2204L, 2205L, 2533L, 2534L, 2535L, 2536L, 
2537L, 2538L, 2539L, 2540L, 2541L, 2542L, 2543L, 2544L, 2545L, 
2546L, 2547L, 2548L, 2549L, 2550L, 2551L, 2552L, 2553L, 2554L, 
2555L, 2556L, 2557L, 2558L, 2559L, 2560L, 2561L, 2562L, 2563L, 
2564L, 2565L, 2566L, 2567L, 2568L, 2569L, 2570L, 2571L, 2572L, 
2573L, 2901L, 2902L, 2903L, 2904L, 2905L, 2906L, 2907L, 2908L, 
2909L, 2910L, 2911L, 2912L, 2913L, 2914L, 2915L, 2916L, 2917L, 
2918L, 2919L, 2920L, 2921L, 2922L, 2923L, 2924L, 2925L, 2926L, 
2927L, 2928L, 2929L, 2930L, 2931L, 2932L, 2933L, 2934L, 2935L, 
2936L, 2937L, 2938L, 2939L, 2940L, 2941L, 3269L, 3270L, 3271L, 
3272L, 3273L, 3274L, 3275L, 3276L, 3277L, 3278L, 3279L, 3280L, 
3281L, 3282L, 3283L, 3284L, 3285L, 3286L, 3287L, 3288L, 3289L, 
3290L, 3291L, 3292L, 3293L, 3294L, 3295L, 3296L, 3297L, 3298L, 
3299L, 3300L, 3301L, 3302L, 3303L, 3304L, 3305L, 3306L, 3307L, 
3308L, 3309L, 3637L, 3638L, 3639L, 3640L, 3641L, 3642L, 3643L, 
3644L, 3645L, 3646L, 3647L, 3648L, 3649L, 3650L, 3651L, 3652L, 
3653L, 3654L, 3655L, 3656L, 3657L, 3658L, 3659L, 3660L, 3661L, 
3662L, 3663L, 3664L, 3665L, 3666L, 3667L, 3668L, 3669L, 3670L, 
3671L, 3672L, 3673L, 3674L, 3675L, 3676L, 3677L, 4005L, 4006L, 
4007L, 4008L, 4009L, 4010L, 4011L, 4012L, 4013L, 4014L, 4015L, 
4016L, 4017L, 4018L, 4019L, 4020L, 4021L, 4022L, 4023L, 4024L, 
4025L, 4026L, 4027L, 4028L, 4029L, 4030L, 4031L, 4032L, 4033L, 
4034L, 4035L, 4036L, 4037L, 4038L, 4039L, 4040L, 4041L, 4042L, 
4043L, 4044L, 4045L), class = "data.frame")

Thanks,

James

6
  • Can you share data surrey_total? Usually, in such situations people use function geom_text_repel from library ggrepel
    – Saurabh
    Commented Jan 6, 2021 at 15:56
  • I can save it as a csv but don't really know how to upload it here? PS - It is the legend, not the geom_text which I am looking to adjust. :-)
    – James
    Commented Jan 6, 2021 at 16:01
  • Use function dput to dump the data and paste it with your question. stackoverflow.com/questions/49994249/example-of-using-dput
    – Saurabh
    Commented Jan 6, 2021 at 16:04
  • Thanks, I have now included it.
    – James
    Commented Jan 6, 2021 at 16:12
  • 1
    Seems like the first line did not come through. Can you paste it again?
    – Saurabh
    Commented Jan 6, 2021 at 16:16

1 Answer 1

2

Welcome to SO. I am posting this also for a suggestion how to create a reproducible example. I am using an inbuilt data set and do some quick data wrangling to get in a structurally similar shape as your data. For the question, it does not matter that we are dealing with dates, nor the actual values for that sake.

I am changing the legend margins, after assigning a name = NULL to the color legend.

Small tips re your code: I discovered several theme calls - this is redundant - try putting all in one single call to theme(). Also, you have used both labs and then xlab, ylab and ggtitles. The last three can be put into labs as arguments: labs(x = , y = , title = )

And - use a new line for every new plot layer (see my code). google "tidyverse style guide" for further code style tips. I am using RStudio and using the add-in package "styler" to fine-tune my code styling.

library(tidyverse)
mtcars %>% 
  group_by(carb) %>%
  summarise(av_disp = mean(disp)) %>%
ggplot(aes(carb, av_disp)) +
  geom_col(aes( fill = carb > 3)) +
  geom_line(aes(color = "7-day average")) +
  scale_color_discrete(name = NULL) +
  theme(legend.margin = margin(-0.5,0,0,0, unit="cm"))
#> `summarise()` ungrouping output (override with `.groups` argument)

Created on 2021-01-06 by the reprex package (v0.3.0)

1
  • 1
    This worked brilliantly thanks. Also thanks to whoever added the links to previous answers.
    – James
    Commented Jan 6, 2021 at 18:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.