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

Changing line colours in gg_miss_var

I am trying to change the line colours for each facet in gg_miss_var.

For example, using the data airquality

#Load data
data("airquality")

#Load libraries
library(ggplot2)

#Create missing data plot using gg plot
gg_miss_var(airquality, Month, show_pct = TRUE) + ylim(0, 100) +
  theme(
    panel.background = element_rect(fill = "white"),
    panel.border = element_blank()) +
  scale_fill_manual(
    values = c("forestgreen", "pink", "blue","yellow","red"))


I get:
enter image description here

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

But that doesn’t change the colours, any ideas?

>Solution :

Just as a reference: While out-of-the-box options are quick and easy when it comes to customizing it might be worthwhile to build up the plot from scratch using ggplot2 where for the data preparation you could use naniar::miss_var_summery:

data("airquality")

library(ggplot2)
library(naniar)

df_miss <- airquality %>% 
  dplyr::group_by(Month) %>% 
  miss_var_summary() |> 
  dplyr::mutate(variable = reorder(variable, pct_miss))

ggplot(df_miss, aes(pct_miss, variable, color = factor(Month))) +
  geom_point() +
  geom_segment(aes(yend = variable, xend = 0)) +
  scale_color_manual(values = c("forestgreen", "pink", "blue","yellow","red"), guide = "none") +
  xlim(0, 100) +
  facet_wrap(~Month) +
  theme_minimal() +
  theme(
    panel.background = element_rect(fill = "white"),
    panel.border = element_blank())

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