0
votes

I have a ggplot that shows relationship between male and female longevity of numerous species with lines. As it is not very clear having so many lines running across, I would like to place a condition on the plot so that if a species male longevity > female longevity then the lines will be black, if not then the lines will be red (i.e. male longevity < or = female longevity).

I am using this data

`MaleFemale.max.longevity    Sex Binomial
195 Male    Agouti_paca
192 Female  Agouti_paca
196 Male    Alopex_lagopus
126 Female  Alopex_lagopus
240 Male    Amblonyx_cinereus
276 Female  Amblonyx_cinereus
254 Male    Aotus_azarai
174 Female  Aotus_azarai
310 Male    Arctictis_binturong
324 Female  Arctictis_binturong
430 Male    Cacajao_calvus
276 Female  Cacajao_calvus
314 Male    Callicebus_moloch
244 Female  Callicebus_moloch
223 Male    Callithrix_pygmaea
181 Female  Callithrix_pygmaea
164 Male    Canis_adustus
130 Female  Canis_adustus`

And currently using this code (which colours the lines according to species)

`r <- ggplot(News, aes(x = Sex, y = log10(MaleFemale.max.longevity), fill = 
Sex)) +
stat_summary(fun.y=mean, geom="point", pch="-", color="white", size=8,
position = position_dodge(width=0.75)) +
geom_point(size=5, alpha=0.6, aes(group=Sex),
position = position_dodge(width=0.75)) +
geom_line(aes(colour = Binomial, group = Binomial), alpha = 0.6)  +
scale_fill_manual(values=c("#969696","#74c476")) +
theme(axis.text.x = element_text(colour = "black", size=30), 
axis.text.y = element_text(colour = "black",size=30),
axis.title.x = element_text(colour = "black",size=30), 
axis.title.y = element_text(colour = "black",size=30),
legend.position = "none") +
labs(y = 'Longevity', title="Polygynous system")
print(r)`

Many thanks for all your help - Nik

1

1 Answers

0
votes

Try this:

library(dplyr)
News <- spread(News, key="Sex", value=MaleFemale.max.longevity) %>%
  mutate(malelonger = Male > Female) %>%
  gather(key="Sex", value= "MaleFemale.max.longevity", c("Male", "Female"))


r=ggplot(News, aes(x = Sex, y = log10(MaleFemale.max.longevity), fill = 
                        Sex)) +
  stat_summary(fun.y=mean, geom="point", pch="-", color="white", size=8,
               position = position_dodge(width=0.75)) +
  geom_point(size=5, alpha=0.6, aes(group=Sex),
             position = position_dodge(width=0.75)) +
  geom_line(aes(colour = malelonger, group = Binomial), alpha = 0.6)  +
  scale_fill_manual(values=c("#969696","#74c476")) +
  theme(axis.text.x = element_text(colour = "black", size=30), 
        axis.text.y = element_text(colour = "black",size=30),
        axis.title.x = element_text(colour = "black",size=30), 
        axis.title.y = element_text(colour = "black",size=30),
        legend.position = "none") +
  labs(y = 'Longevity', title="Polygynous system")+
  scale_colour_manual(values=c("red2", "black"))
print(r)