1

Using the code below, I have created the below chart. To make it easier for people to see the pattern, I'd like to order states from left to right according to the y values (Dx) by age 65.

Thanks,

NM

Here is my data:

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")) library(tidyverse)

library(tidyverse)

   CAPS_2019 %>% 
  group_by(Age, PopName) %>% 
  mutate(PopName1 = sum(Dx)) %>% 
  ungroup() %>% 
  ggplot(aes(x = fct_reorder(PopName, PopName1), y = Dx, fill = factor(as.character(Age)))) +
  geom_col(position = position_stack(reverse = TRUE)) +
  theme_classic()+
  coord_flip()+
  labs(x = "State", y = "Deaths (%)", caption = (""), face = "bold", fill = "Age") 

enter image description here

Nader Mehri
  • 514
  • 1
  • 5
  • 21
  • 1
    Can't replicate your example because I don't have your data. But you can convert the variables that you map to x and y to factors whose levels correspond to the order that you want. Useful functions are `factor()` and `fct_reorder()`. – AlbertRapp Jun 05 '22 at 18:50
  • 1
    Please share your data using `dput`? – Quinten Jun 05 '22 at 18:51
  • 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")) – Nader Mehri Jun 05 '22 at 20:23
  • Change the dataframe to `CAPS_2019_data` e.g. : `CAPS_2019_data %>% group_by(Age, P.......` – TarJae Jun 06 '22 at 16:09

1 Answers1

1

Update 2 Try this in your new dataset Age and Popname are already factors. So maybe this should work as expected:

CAPS_2019_data %>% 
  group_by(Age, PopName) %>% 
  mutate(PopName1 = sum(Dx)) %>% 
  ungroup() %>% 
  ggplot(aes(x = reorder(PopName, PopName1), y = Dx, fill = Age)) +
  geom_col(position = position_stack(reverse = TRUE)) +
  theme_classic()+
  coord_flip()+
  labs(x = "State", y = "Deaths (%)", caption = (""), face = "bold", fill = "Age") 

Update: 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"))
  1. To get the stacks ordered use position = position_stack(reverse = TRUE)

  2. To order y axis do some preprocessing with group_by and sum and use fct_reorder from forcats package (it is in tidyverse)


library(tidyverse)

CAPS_2019 %>% 
  group_by(Age, PopName) %>% 
  mutate(PopName1 = sum(Dx)) %>% 
  ungroup() %>% 
  ggplot(aes(x = fct_reorder(PopName, PopName1), y = Dx, fill = factor(as.character(Age)))) +
  geom_col(position = position_stack(reverse = TRUE)) +
  theme_classic()+
  coord_flip()+
  labs(x = "State", y = "Deaths (%)", caption = (""), face = "bold", fill = "Age") 

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66