Adding a custom ggplot legend for my plot

Hi there!

I have the following plot. I want to add a custom legend for each of my lines and distributions. Currently as you can see it only shows the Normal Distribution. I have tried making changes but no luck so far. Any ideas?

df_standings <-
data.frame(
  stringsAsFactors = FALSE,
              Rank = c(1L,2L,3L,4L,5L,6L,7L,8L,
                       9L,10L,11L,12L,13L,14L,15L,16L,17L,18L,19L,
                       20L,21L,22L,23L,24L,25L,26L,27L,28L,29L,30L,31L,
                       32L,33L,34L,35L,36L,37L,38L,39L,40L,41L,42L,
                       43L,44L,45L,46L,47L,48L,49L,50L,51L,52L,53L,54L,
                       55L,56L,57L,58L,59L,60L,61L,62L,63L,64L,65L,
                       66L,67L,68L,69L,70L,71L,72L,73L,74L,75L,76L,
                       77L,78L,79L,80L,81L,82L,83L,84L,85L,86L,87L,88L,
                       89L,90L,91L,1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,
                       11L,12L,13L,14L,15L,16L,17L,18L,19L,20L,21L,
                       22L,23L,24L,25L,26L,27L,28L,29L,30L,31L,32L,33L,
                       34L,35L,36L,37L,38L,39L,40L,41L,42L,43L,44L,
                       45L,46L,47L,48L,49L,50L,51L,52L,53L,54L,55L,56L,
                       57L,58L,59L,60L,61L,62L,63L,64L,65L,66L,67L,
                       68L,69L,70L,71L,72L,73L,74L),
            Player = c("BizzNess",
                       "cigarettesaftershrek","gazmon48","andrw1232","C_S_W____","claudioh",
                       "Cana-Brava","Azja","LSC1998","MTGHolic","Jack9Em",
                       "MeninoNey","Junkmener","musasabi","YungDingo",
                       "1337Waffles","Felix_gao","TBagTom","Marcuzinho","kyon1024",
                       "Jiliac","EvolveMTG","nahuel10","MagicTonga",
                       "fingers1991","Healthcardotgov","dingzhen","HelpfulHobo",
                       "Kazuga","DizzyJacob","Venom1","GanYanCHN","SmoothBlue56",
                       "Univerce","occulticplus","Sliverdude","BERNASTORRES",
                       "AzureMoon","Meal","ItsDave","EddDeedoo","Raydan",
                       "Betrix2688","Asuteka114","jibeta","jvidarte",
                       "albertoSD","kaching","jimmerjammer","Soon","madechai",
                       "ChimmyNorbit","Misplacedginger","Magic_Dads71522","FedeG",
                       "dwnb","DreamsOfAshiok","DragonFodder","kuangxiu",
                       "RogueStrix254","KweenKatilda","Slasher21",
                       "tibalt_of_red_sub","SinKarma","Drumogg","xfile","ryuumei",
                       "jdempewolf","SvenSveeterSven","Ant_Are_MTG","bunnyfat",
                       "HotGravyStains","Haqy6510","karatedom","cotomira",
                       "soggymeatball","InterludeShadow","Oderus Urungus",
                       "Linden_43","OnlyLandsDotCom","HouseOfManaMTG","SafetyHazard",
                       "lazerbeam","DarthStone","HIGH_APM_MILF",
                       "John123321","ohno_myQueen","Xenowan","cocof","Cobberz",
                       "Dictator_4_Life","Venom1","Marcuzinho","Margin_Call",
                       "marito9911","Cachorrowo","tibalt_of_red_sub","Spaz696969",
                       "bcs8995","Ale_Mtg","Jack9Em","YungDingo","musasabi",
                       "Skeptic2304","Rvng","C_S_W____","Kazuga","Jiliac",
                       "spiderbyte12","varal","JDRBurk","powermeister",
                       "gamertoad","DobleP","asphdel","crazybaloth","AKane_",
                       "xfile","Ashbrown","MagicTonga","BryanOlvera1",
                       "connormc02","felider","CrazyMorango","Healthcardotgov",
                       "Add1ct3d","DarthStone","Shadowz2005","rastaf","eggybenny",
                       "fingers1991","KEnODvT","Danny_Bambino","Patriones",
                       "UwUsaurio","Xcelar8","DethFrmAbove","La-z-chicken",
                       "Magic_Dads71522","Stanshine","madechai","mistercoral",
                       "Ulrikk","TheWatchWander","JayceWong","EddDeedoo",
                       "InsaneAP3Z","MaxProfit","PlayaPeaimp","andrw1232",
                       "ZYURYO","nahuel10","Coronach","soggymeatball",
                       "Slasher21","karatedom","Ant_Are_MTG","ezgpoison","gazmon48",
                       "TheWhiteNinja62","ScreenwriterNY","ShogunBattosai",
                       "markdm","nitang","Univerce"),
            Points = c(21L,18L,18L,18L,18L,18L,
                       15L,15L,15L,15L,15L,15L,15L,15L,15L,15L,15L,15L,
                       15L,15L,12L,12L,12L,12L,12L,12L,12L,12L,12L,
                       12L,12L,12L,12L,12L,12L,12L,12L,9L,9L,9L,9L,
                       9L,9L,9L,9L,9L,9L,9L,9L,6L,6L,6L,6L,6L,6L,6L,
                       6L,6L,6L,6L,6L,6L,6L,6L,6L,3L,3L,3L,3L,3L,
                       3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,0L,0L,0L,
                       0L,0L,0L,0L,0L,0L,0L,18L,18L,18L,18L,18L,15L,
                       15L,15L,15L,15L,15L,15L,15L,15L,15L,15L,12L,
                       12L,12L,12L,12L,12L,12L,12L,12L,12L,12L,12L,12L,
                       12L,12L,12L,9L,9L,9L,9L,9L,9L,9L,9L,9L,6L,
                       6L,6L,6L,6L,6L,6L,6L,6L,6L,3L,3L,3L,3L,3L,
                       3L,3L,3L,3L,3L,3L,3L,0L,0L,0L,0L,0L,0L,0L,0L,
                       0L,0L,0L),
              OMWP = c(0.5238,0.619,0.619,0.5238,
                       0.5102,0.4653,0.6293,0.615,0.6088,0.5946,0.5884,
                       0.585,0.5816,0.5782,0.5476,0.5374,0.5374,0.534,0.4966,
                       0.4694,0.7102,0.6701,0.6497,0.5986,0.5986,0.5776,
                       0.5675,0.5646,0.5544,0.5544,0.5544,0.4993,0.4932,
                       0.4898,0.4796,0.4694,0.4109,0.5884,0.5714,0.5306,
                       0.5299,0.5238,0.5238,0.5111,0.4993,0.4913,0.4683,
                       0.3667,0.3469,0.6095,0.6032,0.5476,0.5476,0.5374,
                       0.5151,0.5119,0.5048,0.4921,0.4898,0.4683,0.4626,
                       0.4345,0.4177,0.4048,0.3857,0.6349,0.6,0.5714,0.5635,
                       0.5556,0.5476,0.5397,0.5357,0.5238,0.519,0.4869,
                       0.4679,0.4667,0.4452,0.4444,0.4206,0.7143,0.6667,
                       0.6429,0.5714,0.5714,0.4603,0.4524,0.4349,0.4229,
                       0.4167,0.6293,0.6293,0.5646,0.5605,0.5333,0.7041,
                       0.6299,0.6231,0.619,0.5986,0.5816,0.5544,0.5238,0.4898,
                       0.4721,0.449,0.6293,0.6293,0.6259,0.6071,0.5891,
                       0.5646,0.5537,0.5238,0.5198,0.5129,0.5034,0.4966,
                       0.4762,0.4683,0.4626,0.449,0.5986,0.5943,0.5782,
                       0.5397,0.5102,0.5034,0.4921,0.4748,0.4127,0.6071,
                       0.5794,0.5595,0.5571,0.5143,0.5048,0.4921,0.4533,0.4444,
                       0.3929,0.7071,0.6357,0.5833,0.5714,0.5397,0.5119,
                       0.5048,0.4905,0.4825,0.4825,0.4181,0.4167,0.7857,
                       0.7143,0.6429,0.5952,0.5571,0.5302,0.5238,0.5,
                       0.4167,0.4127,0.381),
               GWP = c(0.8235,0.65,0.6316,0.8,0.6,
                       0.6842,0.6111,0.6667,0.6316,0.625,0.6667,0.6471,
                       0.6,0.7059,0.6111,0.6471,0.55,0.6316,0.55,0.5556,
                       0.5556,0.5,0.6111,0.5263,0.5,0.5556,0.5,0.5294,
                       0.5294,0.5294,0.5263,0.5882,0.5,0.5882,0.5,0.5556,
                       0.5789,0.5294,0.5,0.4706,0.4444,0.4706,0.4706,0.5,
                       0.4737,0.5,0.5,0.5,0.5,0.3846,0.4375,0.5455,0.5,
                       0.3889,0.375,0.5,0.4615,0.4,0.3529,0.4375,0.3529,
                       0.6,0.4444,0.4545,0.4615,0.375,0.3333,0.3333,0.1667,
                       0.4286,0.4444,0.4286,0.3333,0.375,0.25,0.4167,
                       0.3,0.3077,0.3,0.375,0.2857,0.3333,0.1429,0.3333,
                       0.3333,0.1429,0.25,0,0.25,0.0909,0.2,0.75,0.7059,
                       0.6316,0.75,0.7222,0.6875,0.5789,0.5789,0.6316,
                       0.5556,0.6875,0.6111,0.5789,0.6667,0.5789,0.6316,
                       0.5263,0.5263,0.5882,0.6,0.5789,0.5294,0.5789,0.625,
                       0.5294,0.5556,0.5625,0.5882,0.5882,0.5625,0.5294,
                       0.55,0.4444,0.5833,0.3889,0.4667,0.4444,0.375,0.3333,
                       0.4667,0.4286,0.4545,0.4375,0.5,0.3889,0.5,0.5385,
                       0.4,0.4167,0.3333,0.4545,0.3636,0.3,0.3333,
                       0.4545,0.375,0.1111,0.2222,0.375,0.375,0.375,0.4,0.3,
                       0.3333,0.2,0.3333,0.25,0.2,0.1429,0.2,0.2,0.2,
                       0.3333,0.2),
              OGWP = c(0.5086,0.5785,0.592,0.4956,
                       0.5057,0.448,0.5578,0.5312,0.5363,0.5448,0.5331,
                       0.5477,0.5621,0.5232,0.5071,0.524,0.5373,0.4907,
                       0.5113,0.4655,0.6188,0.6236,0.5483,0.5622,0.5644,
                       0.5566,0.556,0.5304,0.5184,0.5093,0.5054,0.4709,0.5071,
                       0.4715,0.4988,0.4573,0.4199,0.5283,0.5157,0.5203,
                       0.4974,0.5284,0.4894,0.4877,0.4865,0.5054,0.4769,
                       0.414,0.3891,0.5776,0.5416,0.5,0.525,0.523,0.491,
                       0.4935,0.4954,0.5027,0.4945,0.4912,0.4762,0.4306,
                       0.4466,0.4306,0.4114,0.5464,0.5497,0.5897,0.5052,
                       0.4967,0.5069,0.4935,0.5147,0.5098,0.5001,0.4865,
                       0.4929,0.519,0.466,0.4553,0.4762,0.6471,0.5839,0.5772,
                       0.5408,0.5392,0.4532,0.4643,0.4687,0.4347,0.4875,
                       0.5676,0.5902,0.5509,0.5231,0.494,0.6015,0.5595,
                       0.5978,0.5391,0.568,0.5174,0.5376,0.5067,0.4778,
                       0.4757,0.4432,0.5923,0.5523,0.5673,0.5628,0.5394,
                       0.5264,0.5023,0.4889,0.49,0.4841,0.4911,0.4811,0.4764,
                       0.4652,0.476,0.4654,0.5632,0.5483,0.5444,0.5017,
                       0.4739,0.5309,0.4835,0.4702,0.4495,0.5938,0.5317,
                       0.5174,0.5212,0.4797,0.4925,0.4822,0.4761,0.4398,
                       0.4151,0.6264,0.6204,0.5438,0.5207,0.505,0.502,0.4991,
                       0.5265,0.4815,0.443,0.3826,0.4225,0.6769,0.5673,
                       0.6275,0.5446,0.585,0.4883,0.5082,0.5228,0.4375,
                       0.4278,0.381)
)

# Calculate mean, median, standard deviation
mean_gwp <- mean(df_standings$GWP)
median_gwp <- median(df_standings$GWP)
std_dev_gwp <- sd(df_standings$GWP)


library(ggplot2)

# Plotting the smoothed distribution of GWP
ggplot(df_standings, aes(x = GWP)) +
  geom_density(fill = "black", alpha = 0.8) +
  
  # Add normal distribution curve using stat_function
  stat_function(fun = dnorm, args = list(mean = mean_gwp, sd = std_dev_gwp),
                aes(color = "Normal Distribution"), size = 1, alpha = 0.3) +
  
  geom_vline(aes(xintercept = mean_gwp), color = "red", linetype = "dashed", size = 1) +
  geom_vline(aes(xintercept = median_gwp), color = "pink", linetype = "dashed", size = 1) +
  geom_vline(xintercept = mean_gwp + std_dev_gwp, color = "green", linetype = "dashed", size = 1) +
  geom_vline(xintercept = mean_gwp - std_dev_gwp, color = "green", linetype = "dashed", size = 1) +
  geom_vline(xintercept = mean_gwp + 2 * std_dev_gwp, color = "purple", linetype = "dashed", size = 1) +
  geom_vline(xintercept = mean_gwp - 2 * std_dev_gwp, color = "purple", linetype = "dashed", size = 1) +
  
  labs(title = "Smoothed Distribution of GWP (n = 106)",
       x = "GWP",
       y = "Density") +
  
  # Set the same breaks for both x and y axes
  scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, by = 0.25)) +
  scale_y_continuous(limits = c(0, 3), breaks = seq(0, 3, by = 0.5 )) +
  
  coord_fixed(ratio = .25) + 
  
  theme_minimal()
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

Created on 2023-12-31 with reprex v2.0.2

Hi @GreyMerchant ,

this is a really customized solution

library(ggplot2)
# Plotting the smoothed distribution of GWP
ggplot(df_standings, aes(x = GWP)) +
  geom_density(fill = "black", alpha = 0.8) +
  # Add normal distribution curve using stat_function
  stat_function(fun = dnorm, args = list(mean = mean_gwp, sd = std_dev_gwp),
                aes(color = "Normal Distribution"), size = 1, alpha = 0.3) +
  
  geom_vline(aes(xintercept = mean_gwp, color = "mean_gwp"), linetype = "dashed", size = 1) +
  geom_vline(aes(xintercept = median_gwp, color = "median_gwp"), linetype = "dashed", size = 1) +
  geom_vline(aes(xintercept = mean_gwp + std_dev_gwp, color = "std_dev_gwp"), linetype = "dashed", size = 1) +
  geom_vline(aes(xintercept = mean_gwp - std_dev_gwp, color = "std_dev_gwp"), linetype = "dashed", size = 1) +
  geom_vline(aes(xintercept = mean_gwp + 2 * std_dev_gwp, color = "std_dev_gwp2"), linetype = "dashed", size = 1) +
  geom_vline(aes(xintercept = mean_gwp - 2 * std_dev_gwp, color = "std_dev_gwp2"), linetype = "dashed", size = 1) +
  labs(title = "Smoothed Distribution of GWP (n = 106)",
       x = "GWP",
       y = "Density") +
  
  # Set the same breaks for both x and y axes
  scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, by = 0.25)) +
  scale_y_continuous(limits = c(0, 3), breaks = seq(0, 3, by = 0.5 )) +
  coord_fixed(ratio = .25) + 
  theme_minimal() +
  scale_color_manual("Custom legend entry", 
                     breaks=c("Normal Distribution", "mean_gwp", "median_gwp", "std_dev_gwp", "std_dev_gwp2"),
                     values=c("Normal Distribution"="red","mean_gwp"="red","median_gwp"="blue", "std_dev_gwp" = "green", "std_dev_gwp2" = "purple"))

2 Likes

This will work for now! Thanks so much :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.