Arranging variables in ggplot

Dear all,

I have extract some variables from different Bayesian models and would like to plot these variables but in order from closer to zero to far from zero (regardless of the negative sign). More specifically, I want to the variables to be ordered/arranged starting from H1*-H2* (closest to the zero line; hence, should the lowest variable in the plot.) to CD % voicing ( furthest from zero). I tried a couple of ways but not sure why it is not working.

Here is my current plot.

t

Here is my code for this plot:

library(tidyverse)
library(bayestestR)
library(ggdist)
library(tidybayes)

all_var1 %>%
  ungroup() %>%
  mutate(.variable = factor(.variable, levels = c("Burst dur",
                                                  
                                                  "CD % voicing",
                                               
                                                  "H1*-H2* on",
                                                
                                                  "Intensity off"
                                                 ))) %>% 
  ggplot(aes(.value, .variable,fill = stat(abs(x) < 0.1))) +
  annotate("rect", xmin = -0.1, xmax =0.1, ymin = -Inf, ymax = Inf, alpha = 0.5) +
  stat_halfeye( scale = 0.5) +
  geom_vline(xintercept = 0) +
  
  geom_vline(xintercept = c(-0.1, 0.1), linetype = "dashed") +
  scale_x_continuous(limits = c(-1.5, 1.5))+theme_bw()+
  
  scale_fill_manual(values = c("gray80", "skyblue"))+
  stat_interval(position = position_nudge(y = -0.2), size = 2)+
  scale_color_brewer() +
  labs(
    x = expression(beta),
    y = element_blank()
  ) +
  theme(panel.grid.minor = element_blank())

And here is an example data:


structure(list(.chain = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                          3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
                          2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 
                          2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 
                          1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), 
               .iteration = c(1L, 2001L, 4001L, 6001L, 8001L, 1001L, 3001L, 
                              5001L, 7001L, 1L, 2001L, 4001L, 6001L, 8001L, 1001L, 3001L, 
                              5001L, 7001L, 1L, 2001L, 4001L, 6001L, 8001L, 1001L, 3001L, 
                              5001L, 7001L, 1L, 2001L, 4001L, 6001L, 8001L, 1001L, 3001L, 
                              5001L, 7001L, 1L, 2001L, 4001L, 6001L, 8001L, 1001L, 3001L, 
                              5001L, 7001L, 1L, 2001L, 4001L, 6001L, 8001L, 1001L, 3001L, 
                              5001L, 7001L, 1L, 2001L, 4001L, 6001L, 8001L, 1001L, 3001L, 
                              5001L, 7001L, 1L, 2001L, 4001L, 6001L, 8001L, 1001L, 3001L, 
                              5001L, 7001L), .draw = c(1L, 2001L, 4001L, 6001L, 8001L, 
                                                       10001L, 12001L, 14001L, 16001L, 18001L, 20001L, 22001L, 24001L, 
                                                       26001L, 28001L, 30001L, 32001L, 34001L, 1L, 2001L, 4001L, 
                                                       6001L, 8001L, 10001L, 12001L, 14001L, 16001L, 18001L, 20001L, 
                                                       22001L, 24001L, 26001L, 28001L, 30001L, 32001L, 34001L, 1L, 
                                                       2001L, 4001L, 6001L, 8001L, 10001L, 12001L, 14001L, 16001L, 
                                                       18001L, 20001L, 22001L, 24001L, 26001L, 28001L, 30001L, 32001L, 
                                                       34001L, 1L, 2001L, 4001L, 6001L, 8001L, 10001L, 12001L, 14001L, 
                                                       16001L, 18001L, 20001L, 22001L, 24001L, 26001L, 28001L, 30001L, 
                                                       32001L, 34001L), .variable = c("Burst dur", "Burst dur", 
                                                                                      "Burst dur", "Burst dur", "Burst dur", "Burst dur", "Burst dur", 
                                                                                      "Burst dur", "Burst dur", "Burst dur", "Burst dur", "Burst dur", 
                                                                                      "Burst dur", "Burst dur", "Burst dur", "Burst dur", "Burst dur", 
                                                                                      "Burst dur", "H1*-H2* on", "H1*-H2* on", "H1*-H2* on", "H1*-H2* on", 
                                                                                      "H1*-H2* on", "H1*-H2* on", "H1*-H2* on", "H1*-H2* on", "H1*-H2* on", 
                                                                                      "H1*-H2* on", "H1*-H2* on", "H1*-H2* on", "H1*-H2* on", "H1*-H2* on", 
                                                                                      "H1*-H2* on", "H1*-H2* on", "H1*-H2* on", "H1*-H2* on", "Intensity off", 
                                                                                      "Intensity off", "Intensity off", "Intensity off", "Intensity off", 
                                                                                      "Intensity off", "Intensity off", "Intensity off", "Intensity off", 
                                                                                      "Intensity off", "Intensity off", "Intensity off", "Intensity off", 
                                                                                      "Intensity off", "Intensity off", "Intensity off", "Intensity off", 
                                                                                      "Intensity off", "CD % voicing", "CD % voicing", "CD % voicing", 
                                                                                      "CD % voicing", "CD % voicing", "CD % voicing", "CD % voicing", 
                                                                                      "CD % voicing", "CD % voicing", "CD % voicing", "CD % voicing", 
                                                                                      "CD % voicing", "CD % voicing", "CD % voicing", "CD % voicing", 
                                                                                      "CD % voicing", "CD % voicing", "CD % voicing"), .value = c(-0.225287, 
                                                                                                                                                  -0.275691, -0.142486, -0.229671, -0.0264848, -0.184286, -0.242129, 
                                                                                                                                                  -0.129553, -0.115761, -0.170075, -0.199806, -0.131726, -0.271132, 
                                                                                                                                                  -0.280033, -0.229398, -0.0628106, -0.128187, -0.243249, -0.0911992, 
                                                                                                                                                  -0.117383, -0.047975, -0.0249557, -0.0413079, -0.0828279, 
                                                                                                                                                  -0.0725453, -0.0399755, -0.0658288, -0.0901041, 0.0618125, 
                                                                                                                                                  -0.0694756, -0.0273947, -0.0156011, -0.0606744, -0.067321, 
                                                                                                                                                  0.0256827, -0.082949, 0.164578, 0.174108, 0.210949, 0.109308, 
                                                                                                                                                  0.21929, 0.193736, 0.205825, 0.175755, 0.185612, 0.15681, 
                                                                                                                                                  0.217789, 0.125588, 0.204242, 0.127793, 0.312515, 0.182988, 
                                                                                                                                                  0.115424, 0.126167, 0.59485, 0.723234, 0.648111, 0.641218, 
                                                                                                                                                  0.659146, 0.709772, 0.650533, 0.712031, 0.663706, 0.859889, 
                                                                                                                                                  0.668708, 0.631265, 0.704815, 0.716607, 0.675019, 0.875101, 
                                                                                                                                                  0.688213, 0.624296)), class = c("grouped_df", "tbl_df", "tbl", 
                                                                                                                                                                                  "data.frame"), row.names = c(NA, -72L), groups = structure(list(
                                                                                                                                                                                      .variable = c("Burst dur", "CD % voicing", "H1*-H2* on", 
                                                                                                                                                                                                    "Intensity off"), .rows = structure(list(1:18, 55:72, 19:36, 
                                                                                                                                                                                                                                             37:54), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                                                                                   "vctrs_vctr", "list"))), row.names = c(NA, -4L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                                                              "tbl", "data.frame"), .drop = TRUE)) -> all_var1

Your input and advice is highly appreciated.
Thanks in advance!

Use a factor function to reorder the variables. forcats has a few useful functions to make this easier:

Thanks @martin.R for this.

I have already tried this package and it seems to produce a good plot but not the one I'm looking for. The variables are arranged from the lowest to the highest with no reference to zero.

t1


all_var1 %>% 
  ggplot(aes(.value,  fct_reorder(.variable, .value),fill = stat(abs(x) < 0.1))) +
  annotate("rect", xmin = -0.1, xmax =0.1, ymin = -Inf, ymax = Inf, alpha = 0.5) +
  stat_halfeye( scale = 0.5) +
  geom_vline(xintercept = 0) +
  
  geom_vline(xintercept = c(-0.1, 0.1), linetype = "dashed") +
  scale_x_continuous(limits = c(-1.5, 1.5))+theme_bw()+
  
  scale_fill_manual(values = c("gray80", "skyblue"))+
  stat_interval(position = position_nudge(y = -0.2), size = 2)+
  scale_color_brewer() +
  labs(
    x = expression(beta),
    y = element_blank()
  ) +
  theme(panel.grid.minor = element_blank())


Here is a similar plot to what I'm looking for - just in case it helps. Adapted from Frontiers | Tracing the Phonetic Space of Prosodic Focus Marking (frontiersin.org).

library(tidyverse)
library(ggdist)

(var_order_df <- group_by(all_var1,
                         .variable) %>%
  summarise(val=abs(mean(.value))) %>% arrange(val))

(ordered_factor_levels <- pull(var_order_df,
                               .variable))

all_var1 %>% 
  mutate(.variable=factor(.variable,levels=ordered_factor_levels)) %>% 
  ggplot(aes(.value,  .variable,fill = stat(abs(x) < 0.1))) +
  annotate("rect", xmin = -0.1, xmax =0.1, ymin = -Inf, ymax = Inf, alpha = 0.5) +
  stat_halfeye( scale = 0.5) +
  geom_vline(xintercept = 0) +
  
  geom_vline(xintercept = c(-0.1, 0.1), linetype = "dashed") +
  scale_x_continuous(limits = c(-1.5, 1.5))+theme_bw()+
  
  scale_fill_manual(values = c("gray80", "skyblue"))+
  stat_interval(position = position_nudge(y = -0.2), size = 2)+
  scale_color_brewer() +
  labs(
    x = expression(beta),
    y = element_blank()
  ) +
  theme(panel.grid.minor = element_blank())

1 Like

Genius!!
Genius!!
Genius!!

Many thanks, @nirgrahamuk! Highly appreciated!

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.