Guest User

Untitled

a guest
Apr 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. library(tidyverse)
  2. library(rcartocolor)
  3.  
  4. rp <- read_csv("rank-project.csv") %>%
  5. drop_na(Phenotype)
  6.  
  7. names(rp) <- c("reanalyzed_by", "orig_author", "phenotype", "sex", "analysis",
  8. "better_than_null", "aic_null", "aic_ord", "aic_prop",
  9. "delta_aic_ord_prop", "abs_delta", "detection")
  10.  
  11. rp$is_juv <- str_detect(rp$sex, "J")
  12. rp$sex <- fct_collapse(rp$sex,
  13. Female = c("F", "JF"),
  14. Male = c("M", "JM"),
  15. Both = "J")
  16.  
  17. rp$sex <- factor(rp$sex, levels = c("Female", "Male", "Both"))
  18. rp$is_juv <- factor(rp$is_juv, levels = c("FALSE", "TRUE"),
  19. labels = c("Adults", "Juveniles"))
  20.  
  21. rp <- rp %>%
  22. mutate(phenotype = str_replace(phenotype, fixed(" (juv)"), ""),
  23. phenotype = fct_reorder(phenotype, desc(abs_delta)))
  24.  
  25. lim <- max(rp$abs_delta, na.rm = TRUE)
  26.  
  27. plot_data <- filter(rp, better_than_null == 1)
  28.  
  29. pd_neg <- plot_data %>%
  30. filter(delta_aic_ord_prop < 0) %>%
  31. rowwise() %>%
  32. mutate(lab = min(delta_aic_ord_prop, -2)) %>%
  33. ungroup()
  34.  
  35. pd_pos <- plot_data %>%
  36. filter(delta_aic_ord_prop > 0) %>%
  37. rowwise() %>%
  38. mutate(lab = max(delta_aic_ord_prop, 2)) %>%
  39. ungroup()
  40.  
  41. # Orange and blue
  42. # fill_cols <- carto_pal(12, "Bold")[c(7, 8, 4)]
  43.  
  44. # Green and purple
  45. fill_cols <- carto_pal(12, "Bold")[c(1, 2, 4)]
  46.  
  47. ggplot() +
  48. geom_col(data = plot_data,
  49. aes(x = phenotype, y = delta_aic_ord_prop,
  50. fill = sex, alpha = is_juv), width = 0.8) +
  51. # annotate(geom = "rect", xmin = 0, xmax = Inf, ymin = -2, ymax = 2,
  52. # fill = "gray20", alpha = 0.5) +
  53. annotate(geom = "segment", x = 0, xend = Inf, y = 0, yend = 0) +
  54. annotate(geom = "segment", x = 0, xend = Inf, y = -2, yend = -2,
  55. lty = 3, color = "gray20") +
  56. annotate(geom = "segment", x = 0, xend = Inf, y = 2, yend = 2,
  57. lty = 3, color = "gray20") +
  58. geom_text(data = pd_pos,
  59. aes(x = phenotype, y = lab,
  60. label = phenotype), nudge_y = 0.5, hjust = 0,
  61. color = "gray50", size = 3.5) +
  62. geom_text(data = pd_neg,
  63. aes(x = phenotype, y = lab,
  64. label = phenotype), nudge_y = -0.5, hjust = 1,
  65. color = "gray50", size = 3.5) +
  66. annotate(geom = "text", label = expression(phantom(x) %<-% Ordinal~Rank~Better),
  67. x = "Sexual swelling length", y = -34, hjust = 0, size = 6) +
  68. annotate(geom = "text", label = expression(Proportional~Rank~Better %->% phantom(x)),
  69. x = "Sexual swelling length", y = 34, hjust = 1, size = 6) +
  70. coord_flip() +
  71. scale_y_continuous(limits = c(-lim - 7, lim + 7),
  72. breaks = sort(c(-2, 2, seq(-30, 30, by = 10)))) +
  73. scale_fill_manual(values = fill_cols, name = "Sex") +
  74. scale_alpha_manual(values = c(1, 0.5), name = NULL) +
  75. labs(x = NULL, y = expression(Delta~"AIC")) +
  76. theme_classic() +
  77. theme(axis.line.y = element_blank(),
  78. axis.text.y = element_blank(),
  79. axis.ticks.y = element_blank(),
  80. axis.text.x = element_text(size = 10),
  81. axis.title.x = element_text(size = 12),
  82. legend.position = "bottom")
  83.  
  84. ggsave("rank-plot-mardi-gras.pdf", width = 11, height = 7, units = "in")
Add Comment
Please, Sign In to add comment