ggplot2图例未出现的原因 [英] Reasons that ggplot2 legend does not appear

查看:1062
本文介绍了ggplot2图例未出现的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图(不成功)在我的R ggplot2图中显示图例,其中涉及多个图.我的数据框df和代码如下:

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")

我查找了以下堆栈流线程以及Google搜索:

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.

我的第一个问题是使用ggplot时不显示图例的原因是什么?第二是如何解决这些原因.原因之一似乎与多个图和aes()的使用有关,但我怀疑还有其他原因.

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.

推荐答案

您要使用完全错误的方式来设置颜色.您已经在多层中将颜色设置为恒定的字符值,而不是将颜色映射为单个层中的变量的值.

You are going about the setting of colour in completely the wrong way. You have set colour to a constant character value in multiple layers, rather than mapping it to the value of a variable in a single layer.

这主要是因为您的数据不是整洁的"(请参阅​​以下)

This is largely because your data is not "tidy" (see the following)

head(df)
  x           a          b          c
1 1 -0.71149883  2.0886033  0.3468103
2 2 -0.71122304 -2.0777620 -1.0694651
3 3 -0.27155800  0.7772972  0.6080115
4 4 -0.82038851 -1.9212633 -0.8742432
5 5 -0.71397683  1.5796136 -0.1019847
6 6 -0.02283531 -1.2957267 -0.7817367

相反,您应该首先重塑数据:

Instead, you should reshape your data first:

df <- data.frame(x=1:10, a=rnorm(10), b=rnorm(10), c=rnorm(10))
mdf <- reshape2::melt(df, id.var = "x")

这会产生更合适的格式:

This produces a more suitable format:

head(mdf)
 x variable       value
1 1        a -0.71149883
2 2        a -0.71122304
3 3        a -0.27155800
4 4        a -0.82038851
5 5        a -0.71397683
6 6        a -0.02283531

这将使ggplot2易于按预期方式使用,将颜色映射到变量的值:

This will make it much easier to use with ggplot2 in the intended way, where colour is mapped to the value of a variable:

ggplot(mdf, aes(x = x, y = value, colour = variable)) + 
    geom_point() + 
    geom_line()

这篇关于ggplot2图例未出现的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆