2

I am plotting a bar and line chart using a background theme from ggthemes. My variables are grouped by an ordered factor that I set. When I don't use the theme, the factors order the way I want them. But when I add a ggtheme, the order for the line changes, as can be seen in the legend. Why is this happening and how do I fix it?

Example code:

   testCount %>%
      ggplot(aes(x = tests)) +
      theme_solarized_2(light = F) + scale_colour_solarized('blue') +
      geom_bar(aes(y = ..prop.., fill = BandType), position = "dodge") +
      stat_ecdf(aes(color = BandType), size = 1) +
      scale_x_continuous(breaks = seq(0, 18, 1)) +
      scale_y_continuous(breaks = seq(0, 1, 0.1), limits = c(0, 1), labels = percent)

Here is my desired output, where factors are ordered in bar and line chart:

And here is the undesired plot, where factor changes order in the line chart: Undesired: factor changes order in the line chart

EDIT: adding theme_solarized_2(light = F) + scale_fill_solarized('blue') + scale_color_solarized('blue') made the factor ordering consistent. Thanks! Fixed!

bscout11
  • 66
  • 8
  • Possible duplicate of [ggplot2: plotting order of factors within a geom](https://stackoverflow.com/questions/9887342/ggplot2-plotting-order-of-factors-within-a-geom) – Maurits Evers Apr 19 '18 at 23:25
  • Also possible duplicate of [Ordering factors in number order for ggplot](https://stackoverflow.com/questions/48991148/ordering-factors-in-number-order-for-ggplot?rq=1) – Maurits Evers Apr 19 '18 at 23:26
  • 4
    Don't think that is a dupe, I think the issue here is that you set `scale_colour_solarized()` but not `scale_fill_solarized()` so you have two different colour palettes happening. – Marius Apr 19 '18 at 23:26
  • Also possible duplicate of [How to change order of boxplots when using ggplot2](https://stackoverflow.com/questions/6867393/how-to-change-order-of-boxplots-when-using-ggplot2/6867418) – Maurits Evers Apr 19 '18 at 23:26
  • @Marius I see. In that case please ignore my dupe flags. – Maurits Evers Apr 19 '18 at 23:27
  • Can you please provide a reproducible example? – Kamil Apr 20 '18 at 00:33
  • 1
    It appears that the plot is correct. The band type is red and blue for "high". Red is for the bar, and blue is for the line. You can manually choose colors for the line, I believe. Another way is to force factoring of your variables. – Kamil Apr 20 '18 at 00:36
  • @Marius That fixed it. Had to add `scale_colour_solarized()` and `scale_fill_solarized(`) and the order fixed itself – bscout11 Apr 20 '18 at 17:00

0 Answers0