Home > Net >  How know exactly what is the correct order of the dataset using set_name function?
How know exactly what is the correct order of the dataset using set_name function?

Time:09-28

Recently I plotted a boxplot with 3 different datasets. The plot is fine. But when I use the function set_names, I set the data in this order: "S", "M", and "E", instead the order is not the same. Here is the code:

df <- 
  list(df_1v, df_2v, df_3v) %>% 
  set_names(c("S", "M", "E")) %>% 
  map_dfr(bind_rows, .id = "df") %>% 
  pivot_longer(-df)

So, here I set the order of the data frames that I use in the same order that when setting the function set_names. However, this is the plot: enter image description here

This plot shows the inverted order: "E", "M", and "S". How can I know if the data is in the correct order without seeing each value of the data frame (the data is enormous)? There is a function to know the exact order?

Only in case do you need it, here is the code for the boxplot:

ggplot(df) 
  geom_boxplot(aes(x = name, y = value), 
               fill = "blue",
               color = "blue",
               alpha = 0.2,
               notch = T,
               notchwidth = 0.8) 
  facet_wrap(~df, nrow = 1)

CodePudding user response:

You can try this code -

library(tidyverse)

list(df_1v, df_2v, df_3v) %>% 
  set_names(c("S", "M", "E")) %>% 
  map_dfr(bind_rows, .id = "df") %>% 
  pivot_longer(-df) %>%
  mutate(df = factor(df, unique(df))) %>%
  ggplot()  
  geom_boxplot(aes(x = name, y = value), 
               fill = "blue",
               color = "blue",
               alpha = 0.2,
               notch = T,
               notchwidth = 0.8)  
  facet_wrap(~df, nrow = 1)

The order of plots is controlled by levels of factor variable in the data. By using factor(df, unique(df)) the levels are assigned based on their occurrence in the data so we get the order as we specified in set_names i.e c("S", "M", "E")

  • Related