Home > Enterprise >  Remove points and dashed line from respective legend item symbols and add space between them
Remove points and dashed line from respective legend item symbols and add space between them

Time:09-17

Based on the data and code below how can I remove geom_points from Average Precipitation and Average Mean Temperature showing in the legend and the dashed line from the Precipitation symbol, and add white space between these legend symbols? And for some reason these legend items symbols show the dashed line as a dashed .

Data (df):

structure(list(year = 1980:2021, AvgTMean = c(46.6861236310086, 
45.9935321876897, 48.1700038729655, 46.4810115090517, 46.4829304907586, 
46.7485468749052, 47.6219529266638, 47.2361218780086, 47.2499555211466, 
47.5220451962069, 48.5570011928017, 48.3781107312672, 46.8573046128362, 
47.176300310069, 48.4485899941034, 47.4609176108276, 46.6484865289397, 
48.1642107206379, 48.1402675745948, 46.9259296469741, 47.0971184616466, 
46.9726183841638, 47.6723684868621, 47.5039107233534, 47.1155762179138, 
46.7073537729569, 47.0220951396465, 47.7124003840862, 47.3834037927155, 
47.1793288212414, 45.2688071177586, 47.9963270432845, 47.1814417165345, 
47.868863306069, 47.5397788474655, 49.6073711623448, 48.3243434869914, 
48.3742754506034, 48.1300366010172, 48.8198359721638, 48.8188162341724, 
48.1362190050517), year.1 = 1980:2021, AvgPpt = c(7.98047681617574, 
4.49794226223406, 4.9684832619452, 5.55716999365373, 4.57494083048695, 
5.14877823483309, 4.97444435545455, 4.22068948662692, 3.60726044275288, 
2.84911801980053, 4.21517699598155, 5.46851140458677, 4.52277085448576, 
4.64293974933608, 6.15600410764903, 5.875274014911, 4.87718798058995, 
5.8443990209339, 5.32429453784799, 5.74763654079229, 4.17193446127319, 
4.76059529730215, 4.16013977314758, 4.69800474088282, 3.72061294666274, 
5.29432854004791, 3.93882676214587, 4.43336743516705, 4.64442420697922, 
5.77322014781625, 5.20579882483319, 4.61210504742011, 5.74595928751707, 
4.42505010181327, 4.9249395747262, 4.47639288282123, 4.93601457652385, 
6.44108245535473, 5.15154468243163, 6.11707801937097, 8.82894831877217, 
5.03274669845953)), class = "data.frame", row.names = c(NA, -42L
))

Code:

ggplot(df, aes(x = AvgTMean, y = AvgPpt))  
  geom_point(aes(color = "Precipitation"), size = 2, shape = 1)  
  geom_smooth(method = lm, aes(linetype = "LM"), se = T, color = "red")    
  xlab("Mean Temperature (\u00B0F)")  
  ylab("Average Precipitation (in)")  
  ggtitle("Precipation - Mean Temperature Correlation Scatterplot")  
  scale_linetype_manual(values = 2, name = NULL)  
  scale_colour_manual(values = c("Average Precipitation" = "blue",
                                 "Average Mean Temperature" = "orange",
                                 "Precipitation" = "black"), name = "Legend")  
  coord_cartesian(clip = "off")  
  guides(color = guide_legend(override.aes = list(alpha = 0.5), order = 1))  
  geom_hline(aes(yintercept = mean(AvgPpt), color = "Average Precipitation"),linetype = "dashed")  
  geom_vline(aes(xintercept = mean(AvgTMean), color = "Average Mean Temperature"),linetype = "dashed")  
  theme(axis.text = element_text(size = 12), 
        legend.text = element_text(size = 12), 
        legend.title = element_text(size = 14),
        axis.title = element_text(size = 14))   

Output:

enter image description here

CodePudding user response:

The easiest way to do this is:

  1. Map "precipitation" to the shape aesthetic to give it a different entry in the legend
  2. Add a scale_shape_manual to give the correct shape
  3. Change the key_glyph for geom_vline to draw_key_path to remove the vertical component of the key glyph
ggplot(df, aes(x = AvgTMean, y = AvgPpt))  
  geom_point(aes(shape = "Precipitation"), size = 2)  
  geom_smooth(method = lm, aes(linetype = "LM"), se = T, color = "red")    
  xlab("Mean Temperature (\u00B0F)")  
  ylab("Average Precipitation (in)")  
  ggtitle("Precipation - Mean Temperature Correlation Scatterplot")  
  scale_linetype_manual(values = 2, name = NULL)  
  scale_colour_manual(values = c("Average Precipitation" = "blue",
                                 "Average Mean Temperature" = "orange"), 
                      name = "Legend")  
  coord_cartesian(clip = "off")  
  guides(color = guide_legend(override.aes = list(alpha = 0.5), order = 1))  
  geom_hline(aes(yintercept = mean(AvgPpt), 
                 color = "Average Precipitation"),
                 linetype = "dashed")  
  geom_vline(aes(xintercept = mean(AvgTMean), 
                 color = "Average Mean Temperature"),
                 linetype = "dashed", key_glyph = draw_key_path)  
  scale_shape_manual(values = 1, name = NULL)  
  theme(axis.text = element_text(size = 12), 
        legend.text = element_text(size = 12), 
        legend.title = element_text(size = 14),
        axis.title = element_text(size = 14)) 

enter image description here

  • Related