Home > Software engineering >  change colour in an R bar plot
change colour in an R bar plot

Time:08-10

here is the structure of my dataset for reproducibility :

 structure(list(type_de_sejour = c("Hospitalisé", "Hospitalisé", 
 "Hospitalisé", "Hospitalisé", "Hospitalisé", "Hospitalisé"
 ), site = c("FRA", "FRA", "FRA", "FRA", "FRA", "FRA"), specialite = c("ANESTHESIE 
 REANIMATION", 
 "Autres", "CARDIO VASCULAIRE", "CHIRUGIE CARDIAQUE", "CHIRURGIE GENERALE ET VISCERALE", 
  "CHIRURGIE THORACIQUE et VASCULAIRE"), proportion = c(0.000844059928254906, 
  0, 0.357986917071112, 0.0880987550116058, 0.00105507491031863, 
  0.0742772736864317), annee = c("2019", "2019", "2019", "2019", 
  "2019", "2019")), row.names = c(NA, 6L), class = "data.frame")

And here is my code for my plot. It gives me a plot with rainbow colours so it's difficult to differentiate the different parts of my stacked bar chart (colour gradient). How can I change the colours, to make it both nice visually and easy to read ?

ggplot(NCN_hosp, aes(x = annee, y = proportion, fill = specialite))  
   geom_bar(stat = "identity")   
   facet_wrap(~site)  
   theme(plot.title = element_text(hjust = 0.5, vjust = 1, size = 8),
         axis.text.x = element_text(angle = 90, hjust = 0.5, size = 5),
         axis.text.y = element_text(size = 5), 
         legend.text = element_text(size = 5),
         legend.key.height = unit(0.1, "cm"), 
         panel.grid.major = element_blank(), 
         panel.grid.minor = element_blank()) 
   scale_y_continuous(labels = scales::percent)

CodePudding user response:

You're using (ggplot2). With this package Colors are defined by this variable, to include into plot code.

     col=c("black", "red","blue","cyan")

As an example, this code generates a plot with several inputs and different Colors.

plot(train_data["2019", 
                c("RV", "pred_HAR_RV","pred_EN_HAR_RV","pred_EN_HAR_RV_RIDGE")], 
     col=c("black", "red","blue","cyan"),
     lwd = c(1,1), 
     main = "Actual vs predicted RVs", 
     legend.loc = "topleft")

For those kind of questions, you should refer directly to the documentation of the package. Best.

CodePudding user response:

you can either use this : scale_fill_manual(values=c("#199999","#269F00","#36B4E9","#A6B4E1","#51B4E1","#F69F10")) or this : scale_fill_manual(values=c("red","green","blue","yellow","orange","gray20","gray40"))

that is, you may use hex code or names, there are also packages that what colors you will use of course is subject of choice. This page may give you some ideas of what colors to use https://r-graph-gallery.com/ggplot2-color.html

ggplot(NCN_hosp, aes(x = annee, y = proportion, fill = specialite))  
  geom_bar(stat = "identity")   
  facet_wrap(~site)  
  theme(plot.title = element_text(hjust = 0.5, vjust = 1, size = 8),
        axis.text.x = element_text(angle = 90, hjust = 0.5, size = 5),
        axis.text.y = element_text(size = 5), 
        legend.text = element_text(size = 5),
        legend.key.height = unit(0.1, "cm"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) 
  scale_y_continuous(labels = scales::percent) 
  scale_fill_manual(values=c("#199999","#269F00","#36B4E9","#A6B4E1","#51B4E1","#F69F10")) 
  scale_fill_manual(values=c("red","green","blue","yellow","orange","gray20","gray40"))

CodePudding user response:

Of course, it depends on what you want, but you could also use standard colors palettes like scale_fill_brewer for example:

library(ggplot2)
ggplot(NCN_hosp, aes(x = annee, y = proportion, fill = specialite))  
  geom_bar(stat = "identity")   
  scale_fill_brewer()  
  facet_wrap(~site)  
  theme(plot.title = element_text(hjust = 0.5, vjust = 1, size = 8),
        axis.text.x = element_text(angle = 90, hjust = 0.5, size = 5),
        axis.text.y = element_text(size = 5), 
        legend.text = element_text(size = 5),
        legend.key.height = unit(0.1, "cm"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) 
  scale_y_continuous(labels = scales::percent)

Please take a look at this site for some good option to change your colors of your bars.

Created on 2022-08-10 by the reprex package (v2.0.1)

  • Related