ggplot - 删除alpha图例

时间:2016-04-05 11:05:59

标签: r ggplot2 legend alpha

我创建了一个包含以下数据集的图:

Locus;Island;AR;Type;Shapetype
MS1;ST;4,6315;MS;NA
MS1;FG;3,9689;MS;NA
MS1;SN;3;MS;NA
MS2;ST;2;MS;NA
MS2;FG;2;MS;NA
MS2;SN;2;MS;NA
MS3;ST;7,5199;MS;NA
MS3;FG;5,5868;MS;NA
MS3;SN;3;MS;NA
MS4;ST;2,9947;MS;NA
MS4;FG;3;MS;NA
MS4;SN;2;MS;NA
MS5;ST;9,0726;MS;NA
MS5;FG;5,6759;MS;NA
MS5;SN;2,963;MS;NA
MS6;ST;6,5779;MS;NA
MS6;FG;5,6842;MS;NA
MS6;SN;2;MS;NA
MS7;ST;2;MS;NA
MS7;FG;1;MS;NA
MS7;SN;1;MS;NA
MS8;ST;3,97;MS;NA
MS8;FG;2,9032;MS;NA
MS8;SN;1;MS;NA
MS9;ST;2;MS;NA
MS9;FG;1,9977;MS;NA
MS9;SN;2;MS;NA
MS10;ST;3,9733;MS;NA
MS10;FG;3,9971;MS;NA
MS10;SN;2;MS;NA
MS11;ST;7,4172;MS;NA
MS11;FG;5,6471;MS;NA
MS11;SN;3;MS;NA
MS12;ST;2;MS;NA
MS12;FG;2;MS;NA
MS12;SN;2;MS;NA
MS13;ST;5,6135;MS;NA
MS13;FG;3;MS;NA
MS13;SN;2;MS;NA
MT;ST;12;MT;NA
MT;FG;3;MT;NA
MT;SN;2;MT;NA
TLR1LA;ST;3,68;TLR;TLR1LA
TLR1LA;FG;4,4;TLR;TLR1LA
TLR1LA;SN;1;TLR;TLR1LA
TLR1LB;ST;3,99;TLR;TLR1LB
TLR1LB;FG;5;TLR;TLR1LB
TLR1LB;SN;1;TLR;TLR1LB
TLR2A;ST;4,9;TLR;TLR2A
TLR2A;FG;5;TLR;TLR2A
TLR2A;SN;2;TLR;TLR2A
TLR2B;ST;5,64;TLR;TLR2B
TLR2B;FG;4;TLR;TLR2B
TLR2B;SN;3;TLR;TLR2B
TLR3;ST;1;TLR;TLR3
TLR3;FG;3;TLR;TLR3
TLR3;SN;3;TLR;TLR3
TLR4;ST;1;TLR;TLR4
TLR4;FG;2,89;TLR;TLR4
TLR4;SN;2;TLR;TLR4
TLR5;ST;2,9;TLR;TLR5
TLR5;FG;2;TLR;TLR5
TLR5;SN;2;TLR;TLR5
TLR21;ST;2,91;TLR;TLR21
TLR21;FG;1;TLR;TLR21
TLR21;SN;1;TLR;TLR21

这是剧情的代码:

ggplot(comb, aes(Island, AR, group = Locus, colour = (factor(Type)))) +
  geom_line(aes(colour = factor(Type), alpha = factor(Type), size = factor(Type))) + 
  scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1)) +
  scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3)) +
  xlab("Island") + 
  ylab("Allelic Richness") +
  scale_x_discrete(labels = c("Santiago", "Fogo", "Sao Nicolau"), 
                   limits = c("ST", "FG", "SN")) +
  geom_point(aes(shape = (factor(Shapetype)))) + 
  scale_shape_manual(values = c(1,2,3,4,5,6,7,8,9,10), 
                     breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
                              "TLR4", "TLR5","TLR21", "MS", "MT")) + 
  scale_colour_manual(values = c("Red","Blue","Black"), 
                      breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
                               "TLR4","TLR5","TLR21", "MS", "MT")) + 
  theme_bw() + 
  labs(shape="Functional", colour="Neutral")

Plot

情节可以,但是,我需要删除为alpha值创建的图例。我曾尝试同时使用+ scale_alpha(guide = 'none')guide = 'none',但它们似乎都没有用(我可能会将它们放在错误的位置)。我怀疑它们不起作用,因为手动调整了alpha值。

2 个答案:

答案 0 :(得分:6)

请注意,这不是一个最小的例子。

请注意,您的alpha图例是您的size图例,但由于尺寸非常相似,因此很难看到。在guide = 'none'scale_alpha_manual中设置scale_size_manual即可删除图例中的该部分。

如果你只在scale_alpha_manual中进行,你实际上可以看到这些行的alpha变为1,因此它按预期工作。所以@Thierry的回答是正确的。

完整代码

ggplot(comb, aes(Island, AR, group = Locus, colour = (factor(Type)))) +
  geom_line(aes(colour = factor(Type), alpha = factor(Type), size = factor(Type))) + 
  scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1), guide = 'none') +
  scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3), guide = 'none') +
  xlab("Island") + 
  ylab("Allelic Richness") +
  scale_x_discrete(labels = c("Santiago", "Fogo", "Sao Nicolau"), 
                   limits = c("ST", "FG", "SN")) +
  geom_point(aes(shape = (factor(Shapetype)))) + 
  scale_shape_manual(values = c(1,2,3,4,5,6,7,8,9,10), 
                     breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
                              "TLR4", "TLR5","TLR21", "MS", "MT")) + 
  scale_colour_manual(values = c("Red","Blue","Black"), 
                      breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
                               "TLR4","TLR5","TLR21", "MS", "MT")) + 
  theme_bw() + 
  labs(shape="Functional", colour="Neutral")

结果

(请注意,我的y轴错误,因为您的数据包含逗号,我很懒。)

enter image description here

答案 1 :(得分:3)

指南="无"应该做的伎俩

ggplot(
  comb, 
  aes(Island, AR, group = Locus, colour = (factor(Type)))
) + 
  geom_line(aes(alpha = factor(Type), size = factor(Type))) + 
  geom_point(aes(shape = factor(Shapetype))) +       
  scale_x_discrete(
    "Island", 
    labels = c("Santiago", "Fogo", "Sao Nicolau"), 
    limits = c("ST", "FG", "SN")
  ) + 
  ylab("Allelic Richness") + 
  scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1), guide = "none") + 
  scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3)) + 
  scale_shape_manual(
    "Functional",
    values = c(1,2,3,4,5,6,7,8,9,10), 
    breaks = c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3","TLR4","TLR5","TLR21", "MS", "MT")
  ) + 
  scale_colour_manual(
    "Neutral",
    values = c("Red","Blue","Black"), 
    breaks = c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3","TLR4","TLR5","TLR21", "MS", "MT")
  ) + 
  theme_bw()