如何用颜色和形状创建图例 [英] How to create a legend with both color and shape

查看:152
本文介绍了如何用颜色和形状创建图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了4 * 2实验设计(4次处理和2次重复处理),并且想要使用4个形状和2个颜色符号来分类ggplot2中的4 * 2实验。我有两个传说,一个是形状,另一个是两个颜色,我想把这两个传说合并成一个传说。我搜索了这篇文章(将颜色和形状的图例组合成一个单一的传说),但仍然没有想法解决这个问题。

I have created a 4*2 experiment design(4 treatments and 2 repeats of each treatment) and wanted to use 4 shapes and 2 colors symbols to classify the 4*2 experiments in ggplot2. I have two legends, one for shapes and the other one for two colors, and I want to combine the two legends into a single legend. I have searched the post(Combine legends for color and shape into a single legend), but still have no idea to solve the problem. Thank you in advance!

My data:
x   y   Treatment   Repeat
75.74907227 73.6    A   1
236.4477148 242.8   A   2
93.88145508 98.5    B   1
66.58028809 67.1    B   2
53.54458984 55.2    C   1
32.34567383 31.9    C   2
210.5494727 201.2520117 D   1
497.5761328 532.715625  D   2

My code:
library("ggplot2")
p<-ggplot(hg,aes(hg$x,hg$y))
p<-p+geom_point(stat = "identity",size=3,aes(shape=factor(hg$Treatment),
                                      colour=factor(hg$Repeat)))+
  scale_shape_manual(name="Treatment",values = c(0, 1, 2, 5),
                     labels=c("A","B","C","D")) +
  scale_colour_manual(name="Repeat",values = c("red","darkgreen"),labels=c("1","2")) +
  ggtitle("hg")+
  coord_equal()+
  scale_x_continuous(breaks = seq(0, 750, 50), limits = c(0, 750),expand = c(0, 0)) +
  scale_y_continuous(breaks = seq(0, 750, 50), limits = c(0, 750),expand = c(0, 0)) + 
  theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position=c(0.85,0.3))+
  geom_abline(intercept = 0, slope = 1,
              colour = "red",size=0.5,show_guide = FALSE)
p


推荐答案

如果您注意到链接问题, group2 是图例中组合的两个组的组合。你需要这样做。此外,您需要确保 scale_shape_manual scale_colour_manual 之间的标签相同。

If you notice in the linked question, group2 is a combination of the two groups that were combined in the legend. You need to do the same. Also, you need to make sure the labels are the same between scale_shape_manual and scale_colour_manual.

hg <- read.table(header=T, text='
                 x   y   Treatment   Repeat
75.74907227 73.6    A   1
236.4477148 242.8   A   2
93.88145508 98.5    B   1
66.58028809 67.1    B   2
53.54458984 55.2    C   1
32.34567383 31.9    C   2
210.5494727 201.2520117 D   1
497.5761328 532.715625  D   2
                 ')
hg$TR <- paste(hg$Treatment, hg$Repeat)

p <- ggplot(hg, aes(x, y, shape=TR, colour=TR))+
  geom_point(stat = "identity", size=3)+
  scale_shape_manual(name="Treatment & Repeat",
                     labels=c("A,1","A,2","B,1","B,2","C,1","C,2","D,1","D,2"),
                     values = rep(c(0, 1, 2, 5), each=2)) +
  scale_colour_manual(name="Treatment & Repeat",
                      labels=c("A,1","A,2","B,1","B,2","C,1","C,2","D,1","D,2"),
                      values = rep(c("red","darkgreen"), 4)) +
  ggtitle("hg")+
  coord_equal()+
  scale_x_continuous(breaks = seq(0, 750, 50), limits = c(0, 750),expand = c(0, 0)) +
  scale_y_continuous(breaks = seq(0, 750, 50), limits = c(0, 750),expand = c(0, 0)) + 
  theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position=c(0.85,0.3))+
  geom_abline(intercept = 0, slope = 1,
              colour = "red",size=0.5,show_guide = FALSE)
p

这篇关于如何用颜色和形状创建图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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