为什么我不能使用ggplot2获得两个图例? [英] Why do I not get two legends using ggplot2?

查看:68
本文介绍了为什么我不能使用ggplot2获得两个图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一些数据点上绘制不同模型的预测线.我想获得一个图例,该图例指示每种颜色属于哪个个人,另一个图例,其每种线颜色属于哪个模型.下面,我分享了一个伪造的可重复性示例:

I am plotting different models' prediction lines over some data points. I would like to get a legend indicating to which individual belongs each point colour and another legend indicating to which model belongs each line colour. Below I share a fake example for reproducibility:

set.seed(123)
df <- data.frame(Height =rnorm(500, mean=175, sd=15),
                 Weight =rnorm(500, mean=70, sd=20),
                 ID = rep(c("A","B","C","D"), (500/4)))

mod1 <- lmer(Height ~ Weight + (1|ID), df)
mod2 <- lmer(Height ~ poly(Weight,2) + (1|ID), df)

y.mod1 <- predict(mod1, data.frame(Weight=df$Weight),re.form=NA) # Prediction of y according to model 1
y.mod2 <- predict(mod2, data.frame(Weight=df$Weight),re.form=NA) # Prediction of y according to model 2

df <- cbind(df, y.mod1,y.mod2)
df <- as.data.frame(df)

head(df)

    Height   Weight ID   y.mod1   y.mod2
1 166.5929 57.96214  A 175.9819 175.4918
2 171.5473 50.12603  B 176.2844 176.3003
3 198.3806 90.53570  C 174.7241 174.7082
4 176.0576 85.02123  D 174.9371 174.5487
5 176.9393 39.81667  A 176.6825 177.7303
6 200.7260 68.09705  B 175.5905 174.8027

首先,我绘制数据点:

Plot_a <- ggplot(df,aes(x=Weight, y=Height,colour=ID)) + 
  geom_point() +
  theme_bw() +
  guides(color=guide_legend(override.aes=list(fill=NA)))

Plot_a

然后,我添加相对于预测模型的线:

Then, I add lines relative to the prediction models:

Plot_b <- Plot_a + 
  geom_line(data = df, aes(x=Weight, y=y.mod1,color='mod1'),show.legend = T) + 
  geom_line(data = df, aes(x=Weight, y=y.mod2,color='mod2'),show.legend = T) +
  guides(fill = guide_legend(override.aes = list(linetype = 0)),
         color=guide_legend(title=c("Model")))

Plot_b

有人知道为什么我没有两个不同的图例,一个标题为 Model ,另一个标题为 ID 吗?

Does anyone know why I am not getting two different legends, one titled Model and the other ID?

我想得到这个

推荐答案

问题在于,在ggplot2中,每种美学只能具有一个比例,也只能具有一个图例.当您仅使用 color aes时,您会得到一个图例.如果您想为同一美学增加多个传说,请查看 ggnewscales 软件包.否则,您必须利用第二种美学.

The issue is that in ggplot2 each aesthetic can only have one scale and only one legend. As you are using only the color aes you get one legend. If you want multiple legends for the same aesthetic have a look at the ggnewscales package. Otherwise you have to make use of a second aesthetic.

我首选的方法类似于@RuiBarradas提出的方法.但是,要紧贴您的方法,可以这样实现:

My preferred approach would be similar to the one proposed by @RuiBarradas. However, to stick close to your approach this could be achieved like so:

  1. 在调用 geom_line 时,代替了 linetype 上的颜色映射.
  2. 将线条的颜色设置为参数,即不在aes内.
  3. 利用 scale_linetype_manual 来获得两个模型的实线.
  4. 使用 guide_legend 修复图例中出现的颜色
  1. Instead of color map on linetype in your calls to geom_line.
  2. Set the colors for the lines as arguments, i.e. not inside aes.
  3. Make use of scale_linetype_manual to get solid lines for both models.
  4. Make use of guide_legend to fix the colors appearing in the legend

library(ggplot2)
library(lme4)
#> Loading required package: Matrix

set.seed(123)
df <- data.frame(Height =rnorm(500, mean=175, sd=15),
                 Weight =rnorm(500, mean=70, sd=20),
                 ID = rep(c("A","B","C","D"), (500/4)))

mod1 <- lmer(Height ~ Weight + (1|ID), df)
mod2 <- lmer(Height ~ poly(Weight,2) + (1|ID), df)

y.mod1 <- predict(mod1, data.frame(Weight=df$Weight),re.form=NA) # Prediction of y according to model 1
y.mod2 <- predict(mod2, data.frame(Weight=df$Weight),re.form=NA) # Prediction of y according to model 2

df <- cbind(df, y.mod1,y.mod2)
df <- as.data.frame(df)
Plot_a <- ggplot(df) + 
  geom_point(aes(x=Weight, y=Height, colour=ID)) +
  theme_bw() +
  guides(color=guide_legend(override.aes=list(fill=NA)))

line_colors <- scales::hue_pal()(2)
Plot_b <- Plot_a + 
  geom_line(aes(x=Weight, y=y.mod1, linetype = "mod1"), color = line_colors[1]) + 
  geom_line(aes(x=Weight, y=y.mod2, linetype = "mod2"), color = line_colors[2]) + 
  scale_linetype_manual(values = c(mod1 = "solid", mod2 = "solid")) +
  labs(color = "ID", linetype = "Model") +
  guides(linetype = guide_legend(override.aes = list(color = line_colors)))

Plot_b

这篇关于为什么我不能使用ggplot2获得两个图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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