boxuancui / DataExplorer

Automate Data Exploration and Treatment
http://boxuancui.github.io/DataExplorer/
Other
512 stars 88 forks source link

Is there an easy way to colour or fill histograms, box plots, density plots with custom colours? #156

Closed kamaulindhardt closed 3 years ago

kamaulindhardt commented 3 years ago

Hi,

This is a great package. However, I was wondering if it could be possible to spice it up a bit by allowing users to make custom colour settings on the plots. E.g. scale_color_manual(palette = "Set2") from colourBrewer?

I have tried on the histogram using the brewer.pal function to get all the colours I need..

nb.cols.71 <- 71
era.af.colors.71 <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols.71)

But get the error

Error: Aesthetics must be either length 1 or the same as the data (72): colour

When using it like this

plot.box <- plot_boxplot(agrofor.biophys.modelling.data.numeric, by = "logRR",
             geom_boxplot_args = list("outlier.color" = "black", outlier.shape = 1, outlier.size = 0.8,
             ggtheme = theme_lucid(), ncol = 3, nrow = 5,
             col = era.af.colors.71,
             title = "boxplot"))

plot.box 

See screenshot!... The problem is that the plot only gives me part of my histograms and not all of them (as you see next page is black)

Screenshot 2021-08-13 at 11 20 15

kamaulindhardt commented 3 years ago

I found a way around by rationalizing and dropping NA before plotting

plot.box.data <- agrofor.biophys.modelling.data.numeric %>%
  rationalize() %>%
  drop_na()
plot.box <- plot_boxplot(plot.box.data, by = "logRR",
             geom_boxplot_args = list("outlier.color" = "black", outlier.shape = 1, outlier.size = 0.8,
             ggtheme = theme_lucid(), ncol = 3, nrow = 5,
             col = (era.af.colors.60),
             title = "boxplot"))

plot.box 

However, not perfect, as I font have descrete color levels for each variable.. How to fix this??

image

kamaulindhardt commented 3 years ago

RESOLVED

I found out that I could manually by generating the colour matrix 0f 36 (variables) x 5 boxplots per variable = 180 colours with 36 levels.

Only one problem here: It only works when plotting all the multiples/facets in one page...!

nb.cols.36 <- 36
era.af.colors.36 <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols.36)

explor.cols.group.36 <- c("#66C2A5", "#66C2A5", "#66C2A5", "#66C2A5", "#66C2A5",
                          "#84B797", "#84B797", "#84B797", "#84B797", "#84B797",
                          "#A2AC8A", "#A2AC8A", "#A2AC8A", "#A2AC8A", "#A2AC8A",
                          "#C0A27C", "#C0A27C", "#C0A27C", "#C0A27C", "#C0A27C",
                          "#DE976F", "#DE976F", "#DE976F", "#DE976F", "#DE976F",
                          "#FC8D62", "#FC8D62", "#FC8D62", "#FC8D62", "#FC8D62",
                          "#E59077", "#E59077", "#E59077", "#E59077", "#E59077",
                          "#CF948C", "#CF948C", "#CF948C", "#CF948C", "#CF948C",
                          "#B998A1", "#B998A1", "#B998A1", "#B998A1", "#B998A1",
                          "#A39CB5", "#A39CB5", "#A39CB5", "#A39CB5", "#A39CB5",
                          "#8DA0CB", "#8DA0CB", "#8DA0CB", "#8DA0CB", "#8DA0CB",
                          "#9F9BC9", "#9F9BC9", "#9F9BC9", "#9F9BC9", "#9F9BC9",
                          "#B197C7", "#B197C7", "#B197C7", "#B197C7", "#B197C7",
                          "#C392C6", "#C392C6", "#C392C6", "#C392C6", "#C392C6",
                          "#D48EC4", "#D48EC4", "#D48EC4", "#D48EC4", "#D48EC4",
                          "#E78AC3", "#E78AC3", "#E78AC3", "#E78AC3", "#E78AC3",
                          "#DA99AC", "#DA99AC", "#DA99AC", "#DA99AC", "#DA99AC",
                          "#CDA996", "#CDA996", "#CDA996", "#CDA996", "#CDA996",
                          "#C0B880", "#C0B880", "#C0B880", "#C0B880", "#C0B880", 
                          "#B3C86A", "#B3C86A", "#B3C86A", "#B3C86A", "#B3C86A",
                          "#A6D854", "#A6D854", "#A6D854", "#A6D854", "#A6D854", 
                          "#B7D84C", "#B7D84C", "#B7D84C", "#B7D84C", "#B7D84C",
                          "#C9D845", "#C9D845", "#C9D845", "#C9D845", "#C9D845",
                          "#DBD83D", "#DBD83D", "#DBD83D", "#DBD83D", "#DBD83D",
                          "#EDD836", "#EDD836", "#EDD836", "#EDD836", "#EDD836",
                          "#FED82F", "#FED82F", "#FED82F", "#FED82F", "#FED82F",
                          "#F9D443", "#F9D443", "#F9D443", "#F9D443", "#F9D443",
                          "#F4D057", "#F4D057", "#F4D057", "#F4D057", "#F4D057",
                          "#EFCC6B", "#EFCC6B", "#EFCC6B", "#EFCC6B", "#EFCC6B",
                          "#EAC87F", "#EAC87F", "#EAC87F", "#EAC87F", "#EAC87F",
                          "#E5C494", "#E5C494", "#E5C494", "#E5C494", "#E5C494",
                          "#DBC09A", "#DBC09A", "#DBC09A", "#DBC09A", "#DBC09A",
                          "#DBC09A", "#DBC09A", "#DBC09A", "#DBC09A", "#DBC09A",
                          "#C7B9A6", "#C7B9A6", "#C7B9A6", "#C7B9A6", "#C7B9A6",
                          "#BCB6AC", "#BCB6AC", "#BCB6AC", "#BCB6AC", "#BCB6AC",
                          "#B3B3B3", "#B3B3B3", "#B3B3B3", "#B3B3B3", "#B3B3B3"
                         )

image

kamaulindhardt commented 3 years ago

I stumbled upon this issue again when I wanted to plot my density distribution diagrams

Error: Aesthetics must be either length 1 or the same as the data (17920): fill

Screenshot 2021-08-15 at 13 09 10

So in the end I only choose one colour. How can this be done?

image
boxuancui commented 3 years ago

Hi, may I know what additional information does color offer in your case? plot_boxplot and plot_density is designed to be monochromic (for now), since I have not encountered an use case for colors. My design principle is that if it doesn't represent additional information, it is redundant.

If you have a valid use case, you can refer to the solution from #147 if it is really necessary.