1

I'm seeking some assistance with modifying the legend in my plot using the data below.

dput(df)
structure(list(Week.Number = 1:16, Dist.18 = c(5331.83038, 14084.08602, 
12219.423585, 14406.407445, 5032.74848, 10820.094835, 16935.546075, 
15387.590625, 16195.21247, 20012.09881, 14057.385255, 5127.14891, 
16241.98523, 12793.21837, 10526.785375, 6014.43878), HIR.18 = c(1098.56001, 
4093.010015, 4372.84498, 4074.22002, 709.70499, 2460.04999, 5037.77501, 
5521.029965, 5463.410025, 6761.34502, 3953.20997, 1189.89, 3663.69006, 
2333.005005, 2289.38001, 1069.740005), V6.18 = c(0, 40.77, 63.505, 
112.63, 52.395, 56.795, 211.115, 75.52, 215.059995, 121.725, 
57.64, 15.35, 140.34, 15.615, 85.66, 31.815), Dist.17 = c(11820.06249, 
18123.592835, 14560.30914, 17193.56009, 7733.785765, 15536.659865, 
8694.08218, 19569.060865, 14153.71578, 18498.63446, 16452.63166, 
16820.32351, 9242.407875, 8857.62039, 2371.09375, 10340.258575
), HIR.17 = c(2693.425035, 4971.474985, 4521.895065, 5561.53997, 
1759.31996, 3924.48, 1893.485, 5571.700035, 3239.94503, 4773.02004, 
5927.174995, 4537.58996, 1618.49499, 2771.84002, 284.56, 2181.749995
), V6.17 = c(15.58, 38.355, 240.355, 354.059995, 1.76, 187.575, 
93.495, 184.925, 88.27, 165.08, 231.075, 171.09, 32.55, 93.88, 
0, 56.19)), .Names = c("Week.Number", "Dist.18", "HIR.18", "V6.18", 
"Dist.17", "HIR.17", "V6.17"), row.names = c(NA, -16L), class = "data.frame")

This code generates the plot.

plot <- ggplot(df, aes(x = Week.Number, y = Dist.18, fill = "2018")) +
  geom_col() +
  geom_line(aes(x = Week.Number, y = Dist.17, fill = "2017"), size = 0.75) +
  geom_point(aes(x = Week.Number, y = Dist.17), size = 0.75) +
  scale_fill_manual("color", values = c("2017" = "black", "2018" = "blue")) +
  scale_x_continuous(breaks = c(1:16)) +
  ylab("Dist") +
  theme_classic() +
  theme(plot.title = element_text(face = "bold"),
        axis.title.x = element_text(face = "bold"),
        axis.title.y = element_text(face = "bold"))

enter image description here

I wish to change the title of the legend to "Season" and modify the key. I'm wondering if it's possible to have two different points in the key. For example, a solid blue square for the label 2018 and a black line for 2017, representing each geom in the plot.

Also, i used fill = in the aes() argument to generate a legend in the first instance. This seems to work, but not sure if it's best practice or not.

Hope I've provided enough information. Any help will be greatly appreciated. Thank you.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • legends are created for each 'aesthetics', you have currently only the `fill` aesthetics. If you want more than one legend, you need to specify several aesthetics, here e.g. `linetype` or `color`. You can specify the title of the legend [as shown here](https://stackoverflow.com/questions/14622421/how-to-change-legend-title-in-ggplot) – tjebo Sep 06 '18 at 07:20

2 Answers2

2

As per my comment above, one legend is created for each 'aesthetics' - you have currently only the fill aesthetics. If you want more than one legend, you need to specify several aesthetics, here e.g. linetype or color.

There are some problems with your code, though.

First, in order to make full use of ggplot's functionality with the aesthetics and grouping, I would recommend putting your data in a long format - currently it's in a wide format. E.g., it might make sense to group by years - you could achieve that to put all values which belong to one measurememt into one column, and have a column specifying the year, and then specify the aes for this 'year- column'.

Furthermore, See comments below

 ggplot(df) + 
# avoid specifying your `aes` in the ggplot main call - 
# specially if you have several plots following. 
# Some people say it's even better to leave it completely empty.
  geom_col(aes(x = Week.Number, y = Dist.18, fill = "2018")) + 
# now here you are currently not really making use of the aes-functionality, 
# because you are only creating an aesthetic for one value, i.e. '2018'
  geom_line(aes(x = Week.Number, y = Dist.17, color = "2017"), size = 0.75) + 
# Here I have changed fill to color
  geom_point(aes(x = Week.Number, y = Dist.17), size = 0.75) +
  scale_fill_manual("your title", values = c("2017" = "black", "2018" = "blue")) + 
# this is to show you that you actually already know 
# how to change your legend title - see the graph :)
  scale_x_continuous(breaks = c(1:16)) +
  ylab ("Dist") +
  theme_classic() 

enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94
0

I guess it would be nice to have one title for both legends:

ggplot(df, aes(x = Week.Number)) +
  geom_col(aes(y = Dist.18, fill = "2018")) +
  geom_line(aes(y = Dist.17, col = "2017"), size = 0.75) +
  geom_point(aes(y = Dist.17, col = "2017")) +
  scale_colour_manual("Season", values = c("2017" = "black")) +
  scale_fill_manual("", values = c("2018" = "blue")) +
  scale_x_continuous(breaks = c(1:16)) +
  ylab ("Dist") +
  theme_classic() +
  theme(plot.title = element_text(face = "bold"),
        axis.title.x = element_text(face = "bold"),
        axis.title.y = element_text(face = "bold")) + 
  theme(legend.margin = margin(-0.8, 0, 0, 0, unit = "cm"))

enter image description here

If you do not want to have point in the legend, just remove col = "2017" from geom_point and you get:

enter image description here

The trick is to remove space between two legends with legend.margin argument in theme.

Adela
  • 1,757
  • 19
  • 37