Tibble By Two Codes

Viewed 17

I'd like to expand this graph to also show the data by men and women. I would like to see geographical inpatient and outpatient information for men compared to the same for women.

Here is the data, available from SAMHSA here: https://www.datafiles.samhsa.gov/dataset/national-survey-drug-use-and-health-2020-nsduh-2020-ds0001:

MHtrt<-data1%>%
mutate (AMHINP2 = as.factor(x=AMHINP2))%>%
mutate (AMHINP2 = recode_factor(.x=AMHINP2,
                             '.'= NA_character_,
                             '1'='Yes',
                             '2' = 'No'))%>%
mutate (AMHOUTP3 = as.factor(x=AMHOUTP3))%>%
mutate (AMHOUTP3 = recode_factor(.x=AMHOUTP3,
                              '.'= NA_character_,
                              '1'='Yes',
                              '2' = 'No'))%>%
mutate (AMHRX2 = as.factor(x=AMHRX2))%>%
mutate (AMHRX2 = recode_factor(.x=AMHRX2,
                               '.'= NA_character_,
                               '1'='Yes',
                               '2' = 'No'))%>%
mutate (AMHTXRC3 = as.factor(x=AMHTXRC3))%>%
mutate (AMHTXRC3 = recode_factor(.x=AMHTXRC3,
                             '.'= NA_character_,
                             '1'='Yes',
                             '2' = 'No'))%>%
mutate (AMHTXND2 = as.factor(x=AMHTXND2))%>%
mutate (AMHTXND2 = recode_factor(.x=AMHTXND2,
                             '.'= NA_character_,
                             '1'='Yes',
                             '2' = 'No'))%>%
mutate (AMHRX2 = as.factor(x=AMHRX2))%>%
mutate (AMHRX2 = recode_factor(.x=AMHRX2,
                             '.'= NA_character_,
                             '1'='Yes',
                             '2' = 'No'))%>%
mutate(IRSEX = as.factor(x=IRSEX))%>% 
mutate(IRSEX = recode_factor(.x = IRSEX, 
                           '1' = "Male", 
                           '2' = "Female"))

Code- Is there a way to add in the sex data to this code?

MHtrt %>%
as_tibble() %>%
select(AMHINP2, AMHOUTP3, AMHRX2, COUTYP4)%>%
rename(Inpatient = AMHINP2,
     Outpatient = AMHOUTP3)%>%
  mutate(across(
    -COUTYP4,
    levels = 1:2),
    COUTYP4 = factor(COUTYP4, labels = c("Large Metro", "Small Metro", "Non Metro"))) %>%
  pivot_longer(Inpatient:Outpatient, names_to = "health", values_to = "disorder") %>%
  group_by(health, COUTYP4) %>%
  summarise(disorder = sum(disorder == "Yes", na.rm = TRUE) / n()) %>%
  ggplot(aes(health, y = disorder, fill = COUTYP4)) +
  geom_col(position = "dodge") +
  scale_y_continuous(labels = scales::percent) +
  theme(legend.key.size = unit(.085, "in")) +
  theme(legend.title = element_text(size = 9)) +
  theme(axis.text.x = element_text(size = 7))+  
  labs(title = "Percentage of Mental Health Treatment by County Size")
0 Answers
Related