Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

125 рядки
3.3KB

  1. ---
  2. title: "Line charts for population data"
  3. output: rmarkdown::html_vignette
  4. vignette: >
  5. %\VignetteIndexEntry{bar-chart-race}
  6. %\VignetteEngine{knitr::rmarkdown}
  7. %\VignetteEncoding{UTF-8}
  8. ---
  9. ```{r setup, include=FALSE}
  10. knitr::opts_chunk$set(echo = TRUE)
  11. library(vizoR)
  12. library(ggplot2)
  13. library(gghighlight)
  14. library(dplyr)
  15. ```
  16. ```{r}
  17. data("us_city_populations")
  18. n_cities = 5
  19. top_cities <- us_city_populations %>% filter(Rank <= n_cities) %>%
  20. select(City, State, Region) %>% distinct()
  21. to_plot <- filter(us_city_populations, City %in% top_cities$City)
  22. to_plot <- us_city_populations
  23. last_ranks <- to_plot %>% filter(Year == max(Year)) %>%
  24. mutate(last_rank = Rank) %>% select(City, last_rank)
  25. to_plot <- left_join(to_plot, last_ranks, by= 'City')
  26. right_axis <- to_plot %>%
  27. group_by(City) %>%
  28. top_n(1, Year) %>%
  29. ungroup() %>%
  30. top_n(n_cities, -last_rank)
  31. ends <- right_axis %>%
  32. pull(Population)
  33. labels <- right_axis %>%
  34. pull(City)
  35. ```
  36. ```{r}
  37. ggplot(to_plot, aes(x=Year, y = Population, group = City, color = Region)) +
  38. geom_line(size=1) +
  39. #geom_text(data = subset(to_plot, Year == 2010), aes(x=Inf, y = Population, label=City), hjust = 1) +
  40. scale_x_continuous("", expand=c(0,0))+
  41. scale_y_continuous("",
  42. labels=scales::comma_format(big.mark = " "),
  43. sec.axis = sec_axis(~ ., breaks = ends, labels = labels ))+
  44. theme_elegant() +
  45. theme(legend.position = "bottom",
  46. plot.margin = unit(c(1,3,1,1), "lines"),
  47. axis.line.y = element_blank(),
  48. axis.line.x = element_blank(),
  49. axis.ticks.x = element_line(),
  50. panel.grid.major.y = element_line(color= 'grey90', size = .2) ) +
  51. gghighlight(max(last_rank) <= n_cities, use_direct_label = FALSE, label_key = City)
  52. ```
  53. ```{r}
  54. data("base_pop")
  55. n_cities = 10
  56. # create the ranks based on census populations
  57. base_pop<- base_pop %>%
  58. group_by( annee) %>%
  59. arrange(desc(population)) %>%
  60. mutate(rang = 1:n()) %>%
  61. ungroup()
  62. top_cities <- base_pop %>% filter(rang <= n_cities) %>%
  63. select(ville, dep , region) %>% distinct()
  64. to_plot <- filter(base_pop, ville %in% top_cities$ville)
  65. #to_plot <- base_pop
  66. last_ranks <- to_plot %>% filter(annee == max(annee)) %>%
  67. mutate(last_rank = rang) %>% select(ville, last_rank)
  68. to_plot <- left_join(to_plot, last_ranks, by= 'ville')
  69. right_axis <- to_plot %>%
  70. group_by(ville) %>%
  71. top_n(1, annee) %>%
  72. ungroup() %>%
  73. top_n(n_cities, -last_rank)
  74. ends <- right_axis %>%
  75. pull(population)
  76. labels <- right_axis %>%
  77. pull(ville)
  78. ```
  79. ```{r}
  80. ggplot(to_plot, aes(x=annee, y = population, group = ville, color = region)) +
  81. geom_line(size=1) +
  82. #geom_text(data = subset(to_plot, Year == 2010), aes(x=Inf, y = Population, label=City), hjust = 1) +
  83. scale_x_continuous("", expand=c(0,0))+
  84. scale_y_continuous("",
  85. labels=scales::comma_format(big.mark = " "),
  86. sec.axis = sec_axis(~ ., breaks = ends, labels = labels ))+
  87. theme_elegant() +
  88. theme(legend.position = "bottom",
  89. plot.margin = unit(c(1,3,1,1), "lines"),
  90. axis.line.y = element_blank(),
  91. axis.line.x = element_blank(),
  92. axis.ticks.x = element_line(),
  93. panel.grid.major.y = element_line(color= 'grey90', size = .2) ) +
  94. gghighlight(max(last_rank) <= n_cities, use_direct_label = FALSE, label_key = ville)
  95. ```