Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reasons that ggplot2 legend does not appear [duplicate]

Tags:

r

legend

ggplot2

I was attempting (unsuccessfully) to show a legend in my R ggplot2 graph which involves multiple plots. My data frame df and code is as follows:

  Individuals        Mod.2        Mod.1          Mod.3
1           2 -0.013473145  0.010859793    -0.08914021
2           3 -0.011109863  0.009503278    -0.09049672
3           4 -0.006465788  0.011304668    -0.08869533
4           5  0.010536718  0.009110458    -0.09088954
5           6  0.015501212  0.005929766    -0.09407023
6           7  0.014565584  0.005530390    -0.09446961
7           8 -0.009712516  0.012234843    -0.08776516
8           9 -0.011282278  0.006569570    -0.09343043
9          10 -0.011330579  0.003505439    -0.09649456

str(df)
    'data.frame':   9 obs. of  4 variables:
     $ Individuals   : num  2 3 4 5 6 7 8 9 10
     $ Mod.2         : num  -0.01347 -0.01111 -0.00647 0.01054 0.0155 ...
     $ Mod.1         : num  0.01086 0.0095 0.0113 0.00911 0.00593 ...
     $ Mod.3         : num  -0.0891 -0.0905 -0.0887 -0.0909 -0.0941 ...

ggplot(df, aes(df$Individuals)) +
    geom_point(aes(y=df[,2]), colour="red") + geom_line(aes(y=df[,2]), colour="red") +
    geom_point(aes(y=df[,3]), colour="lightgreen") + geom_line(aes(y=df[,3]), colour="lightgreen") +
    geom_point(aes(y=df[,4]), colour="darkgreen") + geom_line(aes(y=df[,4]), colour="darkgreen") +
    labs(title = "Modules", x = "Number of individuals", y = "Mode")

I looked up the following stackflow threads, as well as Google searches:

  • Merging ggplot2 legend
  • ggplot2 legend not showing
  • `ggplot2` legend not showing label for added series
  • ggplot2 legend for geom_area/geom_ribbon not showing
  • ggplot and R: Two variables over time
  • ggplot legend not showing up in lift chart
  • Why ggplot2 legend not show in the graph
  • ggplot legend not showing up in lift chart. This one was created 4 days ago

This made me realize that making legends appear is a recurring issue, despite the fact that legends usually appear automatically.

My first question is what are the causes of a legend to not appear when using ggplot? The second is how to solve these causes. One of the causes appears to be related to multiple plots and the use of aes(), but I suspect there are other reasons.

like image 959
MBorg Avatar asked Feb 13 '18 13:02

MBorg


People also ask

Why doesn't my Ggplot have a legend?

If you want to add a legend to a ggplot2 chart you will need to pass a categorical (or numerical) variable to color , fill , shape or alpha inside aes . Depending on which argument you use to pass the data and your specific case the output will be different.

How do I add a legend in ggplot2?

You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left . To put it inside the plot area, specify a vector of length 2, both values going between 0 and 1 and giving the x and y coordinates.

Is Ggplot different from ggplot2?

You may notice that we sometimes reference 'ggplot2' and sometimes 'ggplot'. To clarify, 'ggplot2' is the name of the most recent version of the package. However, any time we call the function itself, it's just called 'ggplot'.

How do I change the legend value in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))


1 Answers

colour= XYZ should be inside the aes(),not outside:

geom_point(aes(data, colour=XYZ)) #------>legend

geom_point(aes(data),colour=XYZ)  #------>no legend

Hope it helps, it took me a hell long way to figure out.

like image 127
SSSSyb Avatar answered Sep 29 '22 09:09

SSSSyb