Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I reproduce this chart in ggplot2?

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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)
)

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading