Mikata-Project / ggthemr

Themes for ggplot2.
886 stars 107 forks source link

Error: Unknown colour name #44

Closed yuliaUU closed 2 years ago

yuliaUU commented 3 years ago

I am trying to apply the dust theme:

df = structure(list(Subject = c("BIOL112_2019W1", "BIOL112_2019W1", 
"BIOL112_2019W1", "BIOL112_2019W1", "BIOL112_2019W1", "CHEM100_2019W1", 
"CHEM100_2019W1", "CHEM100_2019W1", "CHEM100_2019W1", "CHEM121_2019W1", 
"CHEM121_2019W1", "CHEM121_2019W1", "CHEM121_2019W1", "CHEM121_2019W1", 
"CHEM121_2019W1", "PHYS100_2019W1", "PHYS100_2019W1", "PHYS100_2019W1", 
"PHYS100_2019W1", "PHYS100_2019W1", "PHYS100_2019W1"), time = c("post", 
"post", "post", "pre", "pre", "post", "post", "pre", "pre", "post", 
"post", "post", "pre", "pre", "pre", "post", "post", "post", 
"pre", "pre", "pre"), cat = c("Anx", "Anx", "Anx", "Anx", "Anx", 
"Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx", 
"Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx"), Survey = c("Man", 
"Non-binary", "Woman", "Man", "Woman", "Man", "Woman", "Man", 
"Woman", "Man", "Non-binary", "Woman", "Man", "Non-binary", "Woman", 
"Man", "Non-binary", "Woman", "Man", "Non-binary", "Woman"), 
    mean_score = c(4.20625, 2.25, 3.72207446808511, 4.11788617886179, 
    3.65895061728395, 4.4375, 4.18103448275862, 4.015625, 3.56451612903226, 
    3.98477157360406, 3.0625, 3.3278463648834, 4.00177935943061, 
    4.9, 3.51446280991736, 3.79565217391304, 3.5, 3.31778425655977, 
    3.96319018404908, 2.85, 3.42198581560284), lower_ci = c(4.00610708103331, 
    -0.762136813856149, 3.59206496072721, 3.96528754103603, 3.57060924917053, 
    3.9625603470315, 3.78725656642336, 3.64998728369789, 3.25023542431441, 
    3.85339501830383, 2.37543805832114, 3.23925134175537, 3.8973161573749, 
    4.14230033953502, 3.43691199959787, 3.63130241468709, 2.4521878436764, 
    3.22338855877664, 3.82418287816304, 1.86272252808857, 3.34043187520304
    ), upper_ci = c(4.40639291896669, 5.26213681385615, 3.852083975443, 
    4.27048481668755, 3.74729198539737, 4.9124396529685, 4.57481239909388, 
    4.38126271630211, 3.87879683375011, 4.11614812890429, 3.74956194167886, 
    3.41644138801143, 4.10624256148631, 5.65769966046498, 3.59201362023684, 
    3.96000193313899, 4.5478121563236, 3.41217995434289, 4.10219748993512, 
    3.83727747191143, 3.50353975600263)), row.names = c(NA, -21L
), groups = structure(list(Subject = c("BIOL112_2019W1", "BIOL112_2019W1", 
"CHEM100_2019W1", "CHEM100_2019W1", "CHEM121_2019W1", "CHEM121_2019W1", 
"PHYS100_2019W1", "PHYS100_2019W1"), time = c("post", "pre", 
"post", "pre", "post", "pre", "post", "pre"), cat = c("Anx", 
"Anx", "Anx", "Anx", "Anx", "Anx", "Anx", "Anx"), .rows = structure(list(
    1:3, 4:5, 6:7, 8:9, 10:12, 13:15, 16:18, 19:21), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

plt<- ggplot(data= df, aes(x = mean_score, y = Survey, xmin = lower_ci, xmax = upper_ci, color= time))+
    geom_errorbar(position = position_dodge(0.3), width = 0.3) +
    geom_point( position = position_dodge(0.3), size = 2.5)+
    facet_grid(Subject~cat)+
   theme(legend.position="top",
         text = element_text(size=18)) 

ggthemr("dust")
plt

but I get an error Error: Unknown colour name: post

I understand that somehow it is not able to apply the color theme to the column that is used for plotting. I am not sure how to specify it

thesixmax commented 3 years ago

I just discovered the same issue and error message while building a shiny app. If I do not load the ggthemr package, the plot works as intended (as is the case in your example).

Without having investigated further, it appears that ggthemr somehow overwrites the color definition in ggplot aes and expects a named color input rather than a factor variable.

tonykim925 commented 3 years ago

I'm having a similar issue as well!

Matt-Int commented 3 years ago

I was having the same issue and may have found a workaround. If used something like plt + scale_color_viridis_d() on the example it would work. Whereas if I used plt + scale_color_discrete() I would get the unknown colour error.

With that in mind I created a function that takes the current swatch() object as a basis for a new pal function:

swatch_pal <- function() {
  function(n) {
    if(n > length(swatch()) - 1) warning(paste("Swatch only has", length(swatch())-1), " colours")
    color_list <- as.character(swatch()[2:length(swatch())])
    return(color_list[1:n])
  }
}

scale_colour_ggthemr_d <- function(...) {
  ggplot2::discrete_scale(
             "colour", "ggthemr_swatch_color",
             swatch_pal(),
             ...
           )
}

scale_color_ggthemr_d <- scale_colour_ggthemr_d

Now I can do plt + scale_colour_ggthemr_d() and it works fine. It will also dynamically change colors if you modify the swatch() object, either through changing theme or creating your own custom palette

Jrehren commented 3 years ago

Had the same issue. Thanks very much, Matt-Int, for solving and sharing!

yasindagasan commented 3 years ago

having the same issue

Matt-Int commented 3 years ago

It looks as if this is caused by ggplot2, as per the issue at tidyverse/ggplot2#4149. So I don't know if there's much that can be done in ggthemr directly.

amy17519 commented 3 years ago

Thanks everyone in this thread for raising this issue.

@Matt-Int, thanks for sharing your solution here. Will have to look into how to have a workaround in ggthemr. I was planning to resubmit to CRAN but then also encountered this issue... I am thinking postponing CRAN submission for now, and resubmit after https://github.com/tidyverse/ggplot2/issues/4149 is solved.

@Matt-Int if you have a solution handy by changing the ggthemr pkg directly, feel free to contribute too!

Amy

Matt-Int commented 3 years ago

Looks like the upstream ggplot2 issue referenced earlier has been closed. I tried the initial code with the latest build of ggplot2 and no longer get the error Error: Unknown colour name: post.

However, the colour is not updated and it seems to use the default ggplot colours instead. I've tried finding what is going on but when I use plt + getOption("ggplot2.discrete.colour")() I get the same (correct) result as when using the plt + scale_colour_ggthemr_d().