1

I've just graphed a number of lines using ggplot2 on a graph and want to add a legend to the graph. I followed the same general procedure as a number of websites show and people suggested on this site but the legend just doesn't seem to be showing up!

Can someone help me on how to make the legend appear in this graph?

The following is the code I used for making the graph and attempting to add a legend:

e<-ggplot(dataset,aes(y=Index_SE))
e1<-e+geom_smooth(aes(x=Professionals),model=lm,colour="black",show.legend=TRUE)
e2<-e1+geom_smooth(aes(x=Health_Social),model=lm,colour="blue",show.legend=TRUE)
e3<-e2+geom_smooth(aes(Manufacturing),model=lm,colour="green",show.legend=TRUE)
e4<-e3+geom_smooth(aes(Machniery_Operators),model=lm,colour="red",show.legend=TRUE)
e5<-e4+ggtitle("Types of Employment vs Index of Socioeconomic Advantage and Disadvantage")
e6<-e5+xlab("Profession Prevelance in LGA(%)")+ylab("Index of Socioeconomic Advantage and Disadvantage")
final<-e6+theme(legend.position=c(35,850),legend.justification=c(35,850))+scale_colour_manual(name="Legend",values=c("Professionals"="black","Health and Social"="blue","Manufacturing"="green","Machinery Operators"="red"))

Graph outputted from code

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Please read the guide on how to create a minimal example (stackoverflow.com/help/mcve) and edit your question ideally using the output of `dput`. Your issue is that your data is in wide form. It needs to be reshaped to long, and then there will be only one call to `geom_smooth` with a `group =` argument. If you can post your data then we can help with this. If you do it this way then the legend will be automatically generated – Conor Neilson Sep 20 '17 at 03:55

1 Answers1

3

As Conor Neilson commented, converting your format from wide to long form will get you a legend with geom_smooth in one line.

But if for some reason you really need to preserve the wide format, you can specify the legend manually, following the example here. Note that colour has to be inside each aes() call, and match the names of scale_colour_manual's values vector:

ggplot(dataset, aes(y = Index_SE)) +
  geom_smooth(aes(x = Professionals, colour = "Professionals"), model=lm) +
  geom_smooth(aes(x = Health_Social, colour = "Health and Social"), model=lm) +
  geom_smooth(aes(x = Manufacturing, colour = "Manufacturing"), model=lm) +
  geom_smooth(aes(x = Machniery_Operators, colour = "Machinery Operators"), model=lm) +
  ggtitle("Types of Employment vs Index of Socioeconomic Advantage and Disadvantage") +
  xlab("Profession Prevelance in LGA (%)") +
  ylab("Index of Socioeconomic Advantage and Disadvantage") +
  theme(legend.position = c(35, 850), 
        legend.justification = c(35, 850)) +
  scale_colour_manual(name = "Legend", 
                      values = c("Professionals" = "black", 
                                 "Health and Social" = "blue",
                                 "Manufacturing" = "green",
                                 "Machinery Operators" = "red"))

I've also removed show.legend = TRUE since by default, the legend will be shown as long as there are mapped aesthetics.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94