Hjalte
Hjalte

Reputation: 396

ggplot - Remove alpha legend

I have created a plot with the following dataset:

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

Here's the code for the plot:

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

The plot is okay, however, I need to remove the legend that is created for the alpha values. I have tried to use both + scale_alpha(guide = 'none')and guide = 'none', but none of them seem to work (I may be placing them in the wrong places, though). I suspect that they do not work, because of the manual adjustment of the alpha values.

Upvotes: 5

Views: 9215

Answers (3)

Jas
Jas

Reputation: 834

Starting with this basic plot

bp <- df %>%
  ggplot(aes(column_of_interest, alpha = 0.25)) + geom_density()

from r cookbook, where bp is your ggplot -

Remove legend for a particular aesthetic (alpha):

bp + guides(alpha="none")

Upvotes: 3

Axeman
Axeman

Reputation: 35272

Please be aware that this is not a minimal example.

Please note that your alpha legend is also your size legend, but this is very hard to see since your sizes are very similar. Set guide = 'none' in both scale_alpha_manual and scale_size_manual to remove that portion of the legend.

If you only do it in scale_alpha_manual you can actually see that the alpha becomes 1 for those lines, so it works as intended. So @Thierry's answer is correct.

Full code

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")

Result

(Note that my y-axis is wrong because your data includes comma's and I was lazy.)

enter image description here

Upvotes: 9

Thierry
Thierry

Reputation: 18487

guide = "none" should do the trick

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()

Upvotes: 6

Related Questions