rggplot2geom-tile

influencing the colour sequence of a stacked bars plot in ggplot


I have a problem that I can't solve myself. I would like to order the colours of the plot (see below) using the variable order, i.e. the way they are listed in the dataframe. Unfortunately, the combination of the argument of geom_tile() position = "stack" and the fact that the variable name does not vary per group causes the results of ggplot to appear in an arbitrary order.

I am very grateful for any help!

Here is my data and code:

Data:

df <- data.frame(
  name = c("sv0_allmain", "sv0_allmain", "sv0_allmain", "sv0_allmain", "sv0_allmain",
           "sv0_allmain", "sv0_allmain", "sv0_allmain", "sv0_skala_posneg_2",
           "sv0_skala_posneg_2", "sv0_skala_posneg_3", "sv0_skala_posneg_3",
           "sv0_skala_posneg_3", "sv0_skala_posneg_4", "sv0_skala_posneg_4",
           "sv0_skala_posneg_4", "sv0_skala_posneg_4", "sv0_skala_posneg_5",
           "sv0_skala_posneg_5", "sv0_skala_posneg_5", "sv0_skala_posneg_5",
           "sv0_skala_posneg_5", "sv0_skala_posneg_6", "sv0_skala_posneg_6",
           "sv0_skala_posneg_6", "sv0_skala_posneg_6", "sv0_skala_posneg_6",
           "sv0_skala_posneg_6", "sv0_skala_posneg_7", "sv0_skala_posneg_7",
           "sv0_skala_posneg_7", "sv0_skala_posneg_7", "sv0_skala_posneg_7",
           "sv0_skala_posneg_7", "sv0_skala_posneg_7", "sv0_mono_rot",
           "sv0_mono_rot", "sv0_mono_eisblau", "sv0_mono_eisblau",
           "sv0_mono_dunkelblau", "sv0_mono_dunkelblau", "sv0_mono_svgold",
           "sv0_mono_svgold", "sv0_mono_mint", "sv0_mono_mint", "sv1_allmain",
           "sv1_allmain", "sv1_allmain", "sv1_allmain", "sv1_allmain"),
  color = c("#195365", "#6896A8", "#ADA58B", "#91BEA0", "#B5BFC5", "#E73F0C", "#9650EB", "#AFD700", "#91BEA0",
            "#6896A8", "#91BEA0", "#ADA58B", "#6896A8", "#91BEA0", "#A2CEB1", "#79A8BA", "#6896A8", "#91BEA0",
            "#A2CEB1", "#ADA58B", "#79A8BA", "#6896A8", "#91BEA0", "#A2CEB1", "#B3DFC2", "#8AB9CC", "#7AA9BA",
            "#6896A8", "#91BEA0", "#A2CEB1", "#B3DFC2", "#ADA58B", "#8AB9CC", "#7AA9BA", "#6896A8", "#E73f0C",
            "#F9D4C8", "#6896A8", "#DDE7EB", "#195365", "#CBD8DC", "#ADA58B", "#ECEBE5", "#91BEA0", "#E6F0E9",
            "#195365", "#AFD700", "#6896A8", "#91BEA0", "#B5BFC5"),
  total_colors = rep(215, 50),
  y_value = rep(0.004651163, 50),
  order = c("sv0_allmain_1", "sv0_allmain_2", "sv0_allmain_3", "sv0_allmain_4", "sv0_allmain_5",
            "sv0_allmain_6", "sv0_allmain_7", "sv0_allmain_8", "sv0_skala_posneg_2_1",
            "sv0_skala_posneg_2_2", "sv0_skala_posneg_3_1", "sv0_skala_posneg_3_2",
            "sv0_skala_posneg_3_3", "sv0_skala_posneg_4_1", "sv0_skala_posneg_4_2",
            "sv0_skala_posneg_4_3", "sv0_skala_posneg_4_4", "sv0_skala_posneg_5_1",
            "sv0_skala_posneg_5_2", "sv0_skala_posneg_5_3", "sv0_skala_posneg_5_4",
            "sv0_skala_posneg_5_5", "sv0_skala_posneg_6_1", "sv0_skala_posneg_6_2",
            "sv0_skala_posneg_6_3", "sv0_skala_posneg_6_4", "sv0_skala_posneg_6_5",
            "sv0_skala_posneg_6_6", "sv0_skala_posneg_7_1", "sv0_skala_posneg_7_2",
            "sv0_skala_posneg_7_3", "sv0_skala_posneg_7_4", "sv0_skala_posneg_7_5",
            "sv0_skala_posneg_7_6", "sv0_skala_posneg_7_7", "sv0_mono_rot_1", "sv0_mono_rot_2",
            "sv0_mono_eisblau_1", "sv0_mono_eisblau_2", "sv0_mono_dunkelblau_1",
            "sv0_mono_dunkelblau_2", "sv0_mono_svgold_1", "sv0_mono_svgold_2", "sv0_mono_mint_1",
            "sv0_mono_mint_2", "sv1_allmain_1", "sv1_allmain_2", "sv1_allmain_3", "sv1_allmain_4",
            "sv1_allmain_5")
)

Plot:


    ggplot(df, aes(x = fct_rev(fct_inorder(name)), y = y_value)) +
    geom_tile(aes(fill = color), position = "stack") +
    coord_flip() +
    labs(
      title = "In svVis::give_theme_and_color_set() integrierte Farbpalletten und Themes\n",
      x = "",
      y = ""
    ) +
    theme_minimal() +
    scale_fill_identity() +
    give_theme_and_color_set("sv0_allmain")[["theme"]] +
    theme(
      axis.text.x = element_blank()
    )

Solution

  • To stack your tiles according to the variable order map order on the group aes:

    library(ggplot2)
    library(forcats)
    
    ggplot(df, aes(x = 1, y = fct_rev(fct_inorder(name)))) +
      geom_tile(aes(fill = color, group = order), position = "stack") +
      labs(
        title = "In svVis::give_theme_and_color_set() integrierte Farbpalletten und Themes\n",
        x = "",
        y = ""
      ) +
      theme_minimal() +
      scale_fill_identity() +
      #give_theme_and_color_set("sv0_allmain")[["theme"]] +
      theme(
        axis.text.x = element_blank()
      )
    #> Warning: Stacking not well defined when not anchored on the axis