在ggplot2中的图例中指定形状

时间:2019-04-05 10:17:23

标签: r ggplot2

我想更改图例,使其显示颜色,但不按形状区分。

以下是一些数据:

 exampledata <- tribble(~subject, ~group, ~PC, ~condition,
    "1", "A", .8, "cond1",
    "1", "A", .4, "cond2",
    "2", "B", .45, "cond1",
    "2", "B", .4, "cond2",
    "3", "A", .6, "cond1",
    "3", "A", .3, "cond2",
    "4", "B", .5, "cond1",
    "4", "B", .34, "cond2",
    "5", "A", .86, "cond1",
    "5", "A", .34, "cond2",
    "6", "B", .45, "cond1",
    "6", "B", .64, "cond2",
    "7", "A", .36, "cond1",
    "7", "A", .23, "cond2",
    "8", "B", .59, "cond1",
    "8", "B", .24, "cond2",
    "9", "A", .8, "cond1",
    "9", "A", .14, "cond2",
    "10", "B", .42, "cond1",
    "10", "B", .74, "cond2",
    "11", "A", .56, "cond1",
    "11", "A", .32, "cond2",
    "12", "B", .65, "cond1",
    "12", "B", .14, "cond2",
    "13", "A", .76, "cond1",
    "13", "A", .63, "cond2",
    "14", "B", .56, "cond1",
    "14", "B", .14, "cond2",
    "15", "A", .66, "cond1",
    "15", "A", .33, "cond2",
    "16", "B", .65, "cond1",
    "16", "B", .24, "cond2",
    "17", "A", .16, "cond1",
    "17", "A", .73, "cond2",
    "18", "B", .29, "cond1",
    "18", "B", .22, "cond2"
    )

以及到目前为止我一直在使用的代码:

 ggplot(exampledata, aes(x=condition, y=PC, shape = condition, color = group))+
   geom_point(aes(group=group), size = 5, position=position_dodge(-.2)) +
   stat_summary(fun.data=mean_cl_boot, geom = "pointrange", aes(shape=condition, fill = group), 
           size=1.5, position = position_dodge(-.2), colour = "grey32") +
   stat_summary(fun.y = mean, aes(group=group, colour=group), geom='line', size=.8, position = position_dodge(-.2)) +
   scale_shape_manual(name = "group",
                 labels = c("A", "B"),
                 values=c(21,24)) +
   scale_colour_manual(name = "group",
                  labels = c("A", "B"),
                  values = c("#F8766D","#00BFC4")) +
   xlab("condition") +
   ylab('%') +
   theme_bw(base_size=15) +
   theme(axis.text = element_text(size = 20),
    axis.title = element_text(size=20),
    legend.title = element_blank(),
    legend.justification=c(1,1), legend.position=c(1,.99))

这将生成附件图,这几乎是我想要的。但是,如您所见,图例中的形状也有所不同。我希望形状相同,最好是正方形。我尝试了this post的建议,并在代码中添加了guides(colour = guide_legend(override.aes = list(shape = 15))),但这给了我带有彩色线条的灰色正方形-我想用指定的颜色填充正方形。我想我快要到那里了,但我不太正确![enter image description here] 2

2 个答案:

答案 0 :(得分:2)

以下代码将图例绘制为简单的彩色圆圈:

ggplot(exampledata, aes(x=condition, y=PC, shape = condition, color = group)) +
  geom_point(aes(group=group), size = 5, position=position_dodge(-.2)) +
  stat_summary(fun.data=mean_cl_boot, geom = "pointrange", aes(shape=condition, fill = group), 
               size = 1.5, position = position_dodge(-.2), colour = "grey32", show.legend = FALSE) +
  stat_summary(fun.y = mean, geom='line', aes(group=group, colour=group),
               size=.8, position = position_dodge(-.2), show.legend = FALSE) +
  scale_shape_manual(name = "group",
                     labels = c("A", "B"),
                     values=c(21,24)) +
  scale_colour_manual(name = "group",
                      labels = c("A", "B"),
                      values = c("#F8766D","#00BFC4")) +
  xlab("condition") +
  ylab('%') +
  theme_bw(base_size=15) +
  theme(axis.text = element_text(size = 20),
        axis.title = element_text(size=20),
        legend.title = element_blank(),
        legend.justification=c(1,1), legend.position=c(1,.99)) +
  guides(shape = FALSE)

这里有两个更改:首先,我在show.legend = FALSE函数中添加了stat_summary,因此它们不再包含在图例中(否则,它们将添加小线几何)。其次,我添加了guides(shape = FALSE),以使形状美感不映射到图例。

答案 1 :(得分:1)

认为两个小的修改就可以解决问题:

show.legend = FALSE在geom_point规范中, 并在末尾引导(shape =“ none”,fill =“ none”)

ggplot(exampledata, aes(x=condition, y=PC, shape = condition, color = group))+
  geom_point(aes(group=group), size = 5, position=position_dodge(-.2),
             show.legend = FALSE) +
  stat_summary(fun.data=mean_cl_boot, geom = "pointrange", aes(shape=condition, fill = group), 
               size=1.5, position = position_dodge(-.2), colour = "grey32") +
  stat_summary(fun.y = mean, aes(group=group, colour=group), geom='line', size=.8, position = position_dodge(-.2)) +
  scale_shape_manual(name = "group",
                     labels = c("A", "B"),
                     values=c(21,24)) +
  scale_colour_manual(name = "group",
                      labels = c("A", "B"),
                      values = c("#F8766D","#00BFC4")) +
  xlab("condition") +
  ylab('%') +
  theme_bw(base_size=15) +
  theme(axis.text = element_text(size = 20),
        axis.title = element_text(size=20),
        legend.title = element_blank(),
        legend.justification=c(1,1), legend.position=c(1,.99))+
  guides(shape="none", fill="none")