-1

The data I am working with is available here, or below:

Behaviour  Repeatability       UCI       LCI Age stage
Activity       0.1890000 0.2470000 0.1600000  PE     A
Activity       0.5500000 0.7100000 0.3900000  PW     B
Activity       0.5100000 0.6300000 0.4000000   A     D
Activity       0.4100000        NA        NA  A1     D
Activity       0.4229638 0.4561744 0.3854906  CA     D
Activity       0.1812492 0.2111999 0.1522250  CY     C
Aggression     0.2620000 0.3030000 0.1960000  PE     A
Aggression     0.3700000 0.3800000 0.3600000  PW     B
Aggression     0.4400000 0.5600000 0.3300000   A     D
Aggression     0.3740000        NA        NA  A1     D
Aggression     0.3212115 0.3471766 0.2801818  CA     D
Aggression     0.5106432 0.5635857 0.4634950  CY     C

Similar to other users (here, and here), I am trying to plot this data so that the line only goes through specific points (in my case, the black points). Note: the orange (A in Age column) and red (A1 in Age column) points are there to illustrate how my results compare to others.

The closest I've been able to get is:

enter image description here

The code to get this plot is:

pd <- position_dodge(0.3)

ggplot(rep, aes(x = stage, y = Repeatability, shape = Behaviour, colour=Age)) + 
  geom_point(position = position_dodge(width = 0.3), size = 3) + 
  geom_line(aes(group=Behaviour), position = position_dodge(width = 0.3))+
  scale_x_discrete(labels=c("A" = "50-70 days old", "B" = "70-365 days old", "C" = "Yearlings (365 days old)", "D" = "Adults (>365 days old)")) +
  scale_colour_manual(values = c("orange", "red", "black", "black", "black", "black"), name = "Study", breaks=c("A","A1","PE","PW", "CA", "CY")) + 
  guides(colour=FALSE)+ 
  geom_errorbar(aes(ymin=LCI, ymax=UCI), position=pd, width=0.1, size=0.5)+ theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        plot.caption = element_text(hjust = 0, vjust = 2.12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        text = element_text(size = 15)) +
  labs(y = "Repeatability (r) ± 95 % CrI", x = "Life stage")

Is there a way I can have geom_line() connect just the black points?

Blundering Ecologist
  • 1,199
  • 2
  • 14
  • 38

2 Answers2

3

as @IceCreamToucan noted, you could accomplish this by filtering to just include. the relevant Age series.

(I am running into an issue where the alignment between the points and lines is not consistent for the last life stage. Not sure how to resolve.)

library(tidyverse)
ggplot(data = rep, aes(x = stage, y = Repeatability)) +
  geom_point(aes(shape = Behaviour, colour=Age),
             position = position_dodge(width = 0.3), size = 3) +
  geom_line(data = rep %>% filter(Age %in% c("PE", "PW", "CA", "CY")),
            aes(group = Behaviour), 
            position = position_dodge(width = 0.3)) +

  # as in OP....
  scale_x_discrete(labels=c("A" = "50-70 days old", "B" = "70-365 days old", "C" = "Yearlings (365 days old)", "D" = "Adults (>365 days old)")) +
  scale_colour_manual(values = c("orange", "red", "black", "black", "black", "black"), name = "Study", breaks=c("A","A1","PE","PW", "CA", "CY"))  +
  guides(colour=FALSE)+
  # excluded because data missing from question text
  # geom_errorbar(aes(ymin=LCI, ymax=UCI), position=pd, width=0.1, size=0.5)+ 
  theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        plot.caption = element_text(hjust = 0, vjust = 2.12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        text = element_text(size = 15)) +
  labs(y = "Repeatability (r) ± 95 % CrI", x = "Life stage")

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • I need to keep the `geom_errorbar` in the plot. As my OP shows, there is data missing and I have to make a plot with the error bars for every other point still showing. When I run the code you've suggested (but with the `geom_errorbar`) the plot does not connect the points still. – Blundering Ecologist Feb 16 '19 at 22:56
  • @IceCreamToucan Everything I am using is in the question. The NA's are "real". What I mean is that, I have two points with NAs that I still need to plot (those red points). They don't have error bar values and that is "real" data. – Blundering Ecologist Feb 16 '19 at 23:08
  • @IceCreamToucan Great catch! I've added that in. – Blundering Ecologist Feb 16 '19 at 23:12
0

I'll post the solution that ended up working for me in case anyone else encounters a similar problem.

I was able to get the desired plot by renaming the variables in the Age column first to change their order. Changing CA to A1 and A1 to A2.

Then, as @IceCreamToucan and @Jon Spring suggested:

my_colors <- 
   tibble(color = c("orange", "black", "red", "black","black", "black"), 
           Age = c("A","A1","A2", "PE","PW", "CY"))

ggplot(rep, 
aes(x = stage, y = Repeatability, shape = Behaviour, colour=Age)) + 
    geom_point(position = position_dodge(width = 0.3), size = 3) + 
    geom_line(aes(group=Behaviour), position = position_dodge(width = 0.3), 
    data = function(x) inner_join(x, my_colors %>% filter(color == 'black')))+  
    scale_x_discrete(labels=c("A" = "50-70 days old", "B" = "70-365 days old", "C" = "Yearlings (365 days old)", "D" = "Adults (>365 days old)"))) + 
    guides(colour=FALSE)+ #removes the legend showing colour
    geom_errorbar(aes(ymin=LCI, ymax=UCI), position=pd, width=0.1, size=0.5)+     
    theme_bw() +
    theme(axis.line = element_line(colour = "black"),
        plot.caption = element_text(hjust = 0, vjust = 2.12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        text = element_text(size = 15)) +
  labs(y = "Repeatability (r) ± 95 % CrI", x = "Life stage")

To then give the desired plot:

enter image description here

E_net4
  • 27,810
  • 13
  • 101
  • 139
Blundering Ecologist
  • 1,199
  • 2
  • 14
  • 38