0

This is my code I currently have to compare responses by year, but I want to be able to add the variable of race to be displayed in the data. Such as stacked data with a legend with different colors to represent race.

data1820$Q8_4 = car::recode(data1820$Q8_4, "1='Yes'; 2 = 'No'; 3 = 'NR'")
data1820$Q8_4 <-
  factor(data1820$Q8_4, levels = c("Yes", "No", "NR"))

#Do you currently have any kind of health coverage
df <-
  data1820 %>% group_by(year) %>% count(Q8_4) %>% na.omit() %>%
  mutate(Percent = n / sum(n) * 100)

g <- ggplot(df, aes(x = Q8_4, y = Percent, fill = year)) +
  geom_bar(stat = 'identity') + 
  facet_grid(cols = vars(year)) + 
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(), 
    strip.background = element_blank(), 
    panel.spacing = unit(4, "lines"),
    text = element_text(family = "Helvetica", 
                    size = 10),
    axis.ticks.x = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    plot.title = element_text(hjust = 0.5, vjust = 2),

    legend.title = element_blank(),
    legend.position = "none",
    plot.margin = margin(10, 10, 10, 10)

  ) +
  labs(title = "Do you currently have any kind of health coverage",
   y = "Percentage",
   fill = "year") +
  scale_fill_manual(values = colors, na.value = "grey") + 
  scale_y_continuous(labels = scales::percent_format(scale = 1)) + 
  scale_linetype_manual(guide = guide_legend(reverse = TRUE))
camille
  • 16,432
  • 18
  • 38
  • 60
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. What variable contains race data? – MrFlick Jun 16 '21 at 18:47
  • Can you modify your code like this? ```ggplot(df, aes(fill=race, y=percent, x=year)) + geom_bar(position="stack", stat="identity")``` – TJ87 Jun 16 '21 at 18:50
  • 1
    Take a look again at the *minimal* part of the [mcve] guidance. If the question isn't about theming, you don't need to include the 15 lines of code related to theme options. Narrowing your code down to just the problem area makes it easier for us to help, and makes your debugging easier – camille Jun 16 '21 at 18:50

0 Answers0