Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reordering by x bar using one value of facet variable

For the dataset below, I’d like to customize my below plot by ordering states according to the total number of deaths experienced by age 65. I tried the below code.

The answer here does not answer my question because my x variables include 50 values whereas the response is helpful for variables with a few values.

Thanks,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

NM

Here is my data:

CAPS_2019 <- structure(list(Age = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L), .Label = c("30", "50", "65"), class = "factor"), Dx = c(3.057, 
7.847, 17.157, 2.851, 8.861, 21.885, 2.521, 7.889, 21.328), PopName = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("AK", "AL", "AR"), class = "factor")), row.names = c(NA, 
-9L), class = c("tbl_df", "tbl", "data.frame"))

My code:

CAPS_plot_facet <- CAPS_2019 %>% 
  ggplot(aes(x = reorder(PopName, Dx), y = Dx, fill = factor(as.character(Age)))) +
  geom_col(position = position_stack(reverse = TRUE)) +
  facet_wrap("Age",scales="free_x")+
  theme_classic()+
  theme(strip.background = element_blank(), strip.text = element_blank())+
  coord_flip()+
  labs(x = "State", y = "Deaths (%)", caption = (""), face = "bold", fill = "Age")
CAPS_plot_facet 

My plot:

enter image description here

>Solution :

Popname is a factor with some levels that already sort the values. You have to rearrange them:

> CAPS_2019$PopName
[1] AK AK AK AL AL AL AR AR AR
Levels: AK AL AR

You can arrange the df by descending order in Age and Dx so the first values in Popname are sorted.

CAPS_2019 %>% arrange(desc(Age),desc(Dx))

# A tibble: 9 x 3
  Age      Dx PopName
  <fct> <dbl> <fct>  
1 65    21.9  AL     
2 65    21.3  AR     
3 65    17.2  AK     
4 50     8.86 AL     
5 50     7.89 AR     
6 50     7.85 AK     
7 30     3.06 AK     
8 30     2.85 AL     
9 30     2.52 AR 

If you extract the unique values of Popname already sorted you can use them to reset the levels as you want. But, because you are using coord_flip() to flip the axis, the levels will be sorted the other way round in your plot. You can fix it in the ggplot code or just sort the levels in ascending order:

CAPS_2019 %>% arrange(desc(Age),Dx) %>% pull(PopName) %>% unique() %>% as.character()
[1] "AK" "AR" "AL"

Then, just edit the Popname column with the new levels:

CAPS_2019$PopName = factor(as.character(CAPS_2019$PopName), # the values
                           CAPS_2019 %>% arrange(desc(Age),Dx) %>% pull(PopName) %>% unique() %>% as.character()  # the levels sorted in ascending
)
[1] AK AK AK AL AL AL AR AR AR
Levels: AK AR AL

Next, plot the graph as it is. Changing the aesthetic for x-axis in ggplot() as: x = Popname

enter image description here

PS: Note if you don’t flip the axis then the plot are sorted in ascending.

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading