How to create a legend with both color and shape

2019-04-16 08:53发布

问题:

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

回答1:

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



标签: r ggplot2 legend