0

I am trying to change the legend title and labels related to the colors. I tried using this answer as a guide, but it didn't seem to have an impact on the legend text. I think it is because I am using a factor, or because I am using a position dodge? The script and outcome are below:

ggplot(ggplot_survey, aes(factor(hurricanes), fill = factor(bldg_flooding))) +
  geom_bar(position = position_dodge2(width = 0.9, preserve = "single")) +
  facet_wrap(facets = vars(C0_time)) +
  scale_color_discrete(labels=c("No", "Yes", "No Response")) +
  labs(title = "Would frequent flooding (building) prompt you to install WCS?", 
       color = "Response",
       x = "Reported Hurricane Exposure",
       y = "Count"
       )

enter image description here

Basically, I want the text above the legend to read "Response", the 0 to be "No", 1 to be "Yes", and NA to be "No Response". Any advice on where I am going wrong?

tchoup
  • 971
  • 4
  • 11
  • Is your data from some package? If not, can you provide a reproducible example, please? – Fla28 Jan 19 '22 at 00:40
  • 1
    I could be wrong, but you use "fill" for the geom_bar, in labs you may need to make it ```fill = "Response"``` – Silentdevildoll Jan 19 '22 at 00:51
  • Yes, @Silentdevildoll has it! I replaced color with fill in both the labs and the scale_color_discrete, and it solved it. I will put the edited code in a comment below. Thanks! – tchoup Jan 19 '22 at 01:36

1 Answers1

0

Per @silentdevildoll's response:

    ggplot(ggplot_survey, aes(factor(hurricanes), fill = factor(bldg_flooding))) +
  geom_bar(position = position_dodge2(width = 0.9, preserve = "single")) +
  facet_wrap(facets = vars(C0_time)) +
  scale_fill_discrete(labels=c("No", "Yes", "No Response")) +
  labs(title = "Would frequent flooding (building) prompt you to install WCS?", 
       fill = "Response",
       x = "Reported Hurricane Exposure",
       y = "Count"
       )

Key was to replace color in both the scale_color_discrete (now scale_fill_discrete), and in the labs() bit. Thanks again!

tchoup
  • 971
  • 4
  • 11