【问题标题】:How to create a legend with both color and shape如何创建具有颜色和形状的图例
【发布时间】:2014-11-12 13:25:10
【问题描述】:

我创建了一个 4*2 实验设计(4 个处理和每个处理 2 次重复),并希望使用 4 个形状和 2 个颜色符号对 ggplot2 中的 4*2 实验进行分类。我有两个图例,一个用于形状,另一个用于两种颜色,我想将这两个图例组合成一个图例。我已经搜索了帖子(Combine legends for color and shape into a single legend),但仍然不知道如何解决问题。提前谢谢!

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

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    如果您在链接的问题中注意到,group2 是图例中组合的两个组的组合。你需要做同样的事情。此外,您需要确保scale_shape_manualscale_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
    

    【讨论】:

      猜你喜欢
      • 2016-07-13
      • 2018-07-07
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      相关资源
      最近更新 更多