ggalt icon indicating copy to clipboard operation
ggalt copied to clipboard

Band ordering in geom_horizon()

Open matthewstifler opened this issue 6 years ago • 1 comments

Hi! I'm trying to run a toy example with geom_horizon(), influenced by the example in the readme and this FlowingData post. I'm trying to visualize price change for food in Russia in the last 17 years, expressed as percentage of '99 price for each item. Here is a sample of my data for a single item:

> prices %>%
    dplyr::filter(Name == "Капуста") %>%
    select(year, price.change.to.99)
# A tibble: 18 x 2
    year price.change.to.99
   <dbl>              <dbl>
 1  1999          1.0000000
 2  2000          0.9959823
 3  2001          1.0570494
 4  2002          1.4953702
 5  2003          0.9613952
 6  2004          0.9608868
 7  2005          1.1282653
 8  2006          0.9610972
 9  2007          1.4789909
10  2008          0.9536047
11  2009          0.9757401
12  2010          1.9075291
13  2011          0.6759494
14  2012          0.9354862
15  2013          0.9714568
16  2014          1.2883655
17  2015          1.0128819
18  2016          0.7611389

Here is the code I'm using to visualize my data:

ggplot(prices, aes(x = year, y = price.change.to.99 - 1)) +
  geom_horizon(bandwidth = 0.1) +
  facet_grid(Name ~ .) +
  theme_minimal() +
  scale_x_continuous(expand = c(0, 0)) +
  theme(axis.text.y = element_blank(),
        strip.text.y = element_text(hjust = 0, angle = 360)
        ) +
  labs(x = NULL, y = NULL) +
  scale_fill_custom_viridis(discrete = TRUE)

And here is the result this code produces: plot

scale_fill_custom_viridis() is a modified scale_fill_viridis() that allows to use diverging palette.

The issue I'm struggling with is the mixed order of bands. For instance, in 1999 each item has value of 1, it is displayed as 5th band, but it's ordered as first band, so it gets the most intense red color, which is absolutely counter-intuitive. The issue is well displayed by the way Водка, the first item is visualized. Its price was increasing since ca. 2011, but in the chart it is displayed as colors of the bands getting colored less intense, which is quite the contrary to what actually is going on. Another example is Свинина item: its price values range from 9th to 7th band, but both of them are ordered higher than 8th band, which results in weird band coloring, making the chart for the item unreadable.

The problem persists with the original scale_fill_viridis() as well as with my modification.

I tried to find where the band ordering is defined in the source of geom_horizon(), to tweak it somehow, but I failed at this task, and I have no idea how to proceed from here. I'd be very grateful if you could look into this. Thanks!

matthewstifler avatar Oct 30 '17 02:10 matthewstifler

@matthewstifler R/geom_horizon.r seems to convert the band to a factor right at the end of processing. As a workaround you can just delete line 142 of that file. That worked for me.

data$band <- factor(data$band) # delete this guy

From a development side, in my opinion, the default should either be numeric or ordered factor, since bands are inherently an "ordered" quantity with respect to horizon charts. I can't think of a possible situation in which they would be considered an unordered category.

chriscarterxyz avatar Jul 20 '20 16:07 chriscarterxyz