将颜色和形状的图例合并为一个图例


75

我正在使用2 x 2研究设计在ggplot中创建图,并希望使用2种颜色和2个符号对我的4种不同治疗组合进行分类。目前,我有2个图例,一个用于颜色,一个用于两种形状。我如何将它们组合成一个图例,所以我有一个蓝色圆圈,一个红色圆圈,一个蓝色三角形和一个读取三角形?

一些数据:

state1 <- c(rep(c(rep("N", 7), rep("Y", 7)), 2))
year <- rep(c(2003:2009), 4)
group1 <- c(rep("C", 14), rep("E", 14))
group2 <- paste(state1, group1, sep = "")
beta <- c(0.16,0.15,0.08,0.08,0.18,0.48,0.14,0.19,0.00,0.00,0.04,0.08,0.27,0.03,0.11,0.12,0.09,0.09,0.10,0.19,0.16,0.00,0.11,0.07,0.08,0.09,0.19,0.10)
    lcl <- c(0.13,0.12,0.05,0.05,0.12,0.35,0.06,0.13,0.00,0.00,0.01,0.04,0.20,0.00,0.09,0.09,0.06,0.06,0.07,0.15,0.11,0.00,0.07,0.03,0.05,0.06,0.15,0.06)
    ucl <- c(0.20,0.20,0.13,0.14,0.27,0.61,0.28,0.27,0.00,1.00,0.16,0.16,0.36,0.82,0.14,0.15,0.13,0.13,0.15,0.23,0.21,0.00,0.15,0.14,0.12,0.12,0.23,0.16)
data <- data.frame(state1,year,group1,group2,beta,lcl,ucl)

情节:

library(ggplot2)
pd <- position_dodge(.65)
ggplot(data = data, aes(x = year, y = beta, colour = state1, group = group2, shape = group1)) +
  geom_point(position = pd, size = 4) +
  geom_errorbar(aes(ymin = lcl, ymax = ucl),colour = "black", width = 0.5, position = pd) +
  scale_colour_hue(name = "Treatment & State",  #Legend label, use darker colors
                   labels = c("Control", "Exclosure"),
                   l = 40) +
  scale_shape(name = "State", labels = c("Non-F", "Flwr"))

在此处输入图片说明


您的错误,y=estimate我估计是Beta吗?
user1317221_G 2012年

shape应该group1吧?
user1317221_G 2012年

所以你想要一个蓝色的圆圈,一个红色的圆圈,obe蓝色三角形一个红色三角形?
user1317221_G 2012年

对代码的问题感到抱歉。我已经更改它,以便y = beta和shape = group1。我想要一个红色圆圈,蓝色圆圈,红色三角形,蓝色三角形。谢谢!
N Brouwer

主要问题是您使用两个不同的向量来描述您的小组,从而产生两个图例。使用您group2给每个组自己的形状/颜色。然后您使用手册选择想要的特定对象
user1317221_G 2012年

Answers:


107

您需要为形状和色标使用相同的namelabels值。

pd <- position_dodge(.65)
ggplot(data = data,aes(x= year, y = beta, colour = group2, shape = group2)) +    
  geom_point(position = pd, size = 4) +
  geom_errorbar(aes(ymin = lcl, ymax = ucl), colour = "black", width = 0.5, position = pd) +
  scale_colour_manual(name = "Treatment & State",
                      labels = c("Control, Non-F", "Control, Flwr", "Exclosure, Non-F", "Exclosure, Flwr"),
                      values = c("blue", "red", "blue", "red")) +   
  scale_shape_manual(name = "Treatment & State",
                     labels = c("Control, Non-F", "Control, Flwr", "Exclosure, Non-F", "Exclosure, Flwr"),
                     values = c(19, 19, 17, 17))

在此处输入图片说明


精彩!感谢您向我介绍scale_colour_manual和scale_shape_manual
N Brouwer

1
只是一点点注意,这与非手动音阶也一样有效
Max Gordon

1
另一个注意事项:对于填充形状,使用scale_fill_manual而不是scale_colour_manual存在问题。为了解决这个问题,它似乎是必要的,以覆盖设置guides(fill = guide_legend(override.aes = list(fill = ...)))为这样一个问题:stackoverflow.com/questions/12488905/...
出栈

8

您所要求的红色圆圈,蓝色圆圈,红色三角形,蓝色三角形:

ggplot(data =data,aes(x= year, y = beta, shape=group2, colour=group2,group = group2)) +
geom_errorbar(aes(ymin = lcl, ymax = ucl),colour = "black", width = 0.5, position = pd) + 
geom_point(position = pd, size = 4) +  
scale_colour_manual(values=c("red","blue","red","blue")) +   
scale_shape_manual(values=c(19,19,17,17)) +
scale_fill_hue(name="Treatment & State",  #Legend label, use darker colors
labels=c("Control", "Exclosure"),l=40)

在此处输入图片说明

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.