Cannot get ggblanket's gg_col to plot X axis in the correct order

Hello!

I've been using ggblanket (a package of ggplot wrapper functions) lately so i can easily create plots, it has been working wonders until recently when I've noticed it was not plotting columns in the correct order (even after arraging the dataset)

For ggplot it's not unusual to plot the X axis using the reorder function, but it doesn't seem to work with ggblanket (! Can't convert a call to a string), if I try to plot an arranged dataset of mtcars it will still plot the order incorrectly, here's an example

mtcars %>% 
  as_tibble(rownames="cars") %>% 
  arrange(desc(disp)) %>% 
  head(5) %>%
  ggblanket::gg_col(
          x = cars),
          y = disp)

At the same time if I use the reorder function on ggplot it goes well as usual:

mtcars %>% 
  as_tibble(rownames="cars") %>% 
  arrange(desc(disp)) %>% 
  head(5) %>%
  ggplot(aes(x = reorder(cars,-disp),
          y = disp,
          stat = "identity")) +
  geom_col()
1 Like

One way to handle this would be to make your x-axis variable (cars in this case) an ordered factor after arranging.

library(tidyverse)
library(ggblanket)

mtcars %>% 
  as_tibble(rownames="cars") %>% 
  arrange(desc(disp)) %>% 
  mutate(cars = factor(cars, levels = unique(cars), ordered = T)) %>%
  head(5) %>%
  ggblanket::gg_col(
    x = cars,
    y = disp)


Created on 2022-12-28 with reprex v2.0.2.9000

3 Likes

worked like a charm! Thanks @scottyd22!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.