Home > front end >  How can I reproduce this chart in ggplot2?
How can I reproduce this chart in ggplot2?

Time:08-12

I would like to replicate the following chart using ggplot2. Any help?

enter image description here

Here's the data. (icv is the LHS bar and afl is the RHS bar)


library(tibble)

vectorcolors <- c("#967D2D", "#333333", "white")

df <- tibble(
reg = c("Duarte","La Vega","Santiago","Hermanas Mirabal",
         "Sanchez Ramírez","María Trinidad Sánchez","Monseñor Nouel",
         "Puerto Plata","Samaná","Espaillat","Valverde",
         "Santiago Rodríguez","Dajabón","Montecristi"), 
icv = c(69.9, 58.8, 57.1, 54.7, 53.9, 53.3, 49.3,
         48.8, 47.0, 45.4, 44.2, 43.2, 42.8, 42.5),
afl = c(799.3, 800.6, 851.1, 711.7, 839.6, 710.9,
         823.2, 912.7, 605.8, 832.7, 692.0, 858.4,
         758.1, 616.6)
)

CodePudding user response:

library(ggplot2)
library(tibble)

colors <- list("sand" = "#967D2D", "gray" = "#333333", "white" = "white")

df <- tibble(
  reg = c("Duarte","La Vega","Santiago","Hermanas Mirabal",
          "Sanchez Ramírez","María Trinidad Sánchez","Monseñor Nouel",
          "Puerto Plata","Samaná","Espaillat","Valverde",
          "Santiago Rodríguez","Dajabón","Montecristi"), 
  icv = c(69.9, 58.8, 57.1, 54.7, 53.9, 53.3, 49.3,
          48.8, 47.0, 45.4, 44.2, 43.2, 42.8, 42.5),
  afl = c(799.3, 800.6, 851.1, 711.7, 839.6, 710.9,
          823.2, 912.7, 605.8, 832.7, 692.0, 858.4,
          758.1, 616.6)
)


df %>%
  ggplot(aes(y = forcats::fct_reorder(reg, icv)))  
  geom_col(aes(x = icv * -1), fill = colors$sand, color = colors$gray)  
  geom_col(aes(x = afl / 10), fill = colors$white, color = colors$gray)  
  geom_text(aes(label = icv, x = icv * -1, hjust = -0.2), color = "white", size = 3)  
  geom_text(aes(label = afl , x = afl / 10, hjust = 1.1), color = colors$gray, size = 3)  
  geom_text(aes(label = reg , x = 1), color = colors$gray, size = 3, hjust = 0)  
  scale_fill_manual(values = c("#182c4f"))  
  scale_color_manual(values = c("red"))  
  labs(fill = NULL, color = NULL, x = NULL, y = NULL)  
  theme_minimal()  
  theme(
    legend.position = "none",
    panel.grid = element_blank(),
    axis.text = element_blank(),
    axis.title.y =  element_text()
  )

enter image description here

  • Related