Issue with editing ggplot2 legend

I have developed a boxplot using the following sample code:

a<- ggplot(na.omit(df1), aes(x=C,
y=E, fill=as.factor(complete),
color=as.factor(complete))) + geom_boxplot()

a + ggtitle("Sample Text") +
ylab("E") + scale_fill_discrete(name = "
Response", labels = c("No", "Yes")) + theme(plot.title =
element_text(face = "bold", hjust=0.5)) + geom_point(position =
position_jitterdodge()) + scale_fill_manual(values = rep(NA, 2))

This results in a sample graph that displays datapoints exactly as I want but results in the legend getting distorted. Is there a way to alter the code to have the legend title as "Response" with "Yes" and 'No" as the legend items?

redefine complete to be a factor variable earlier in your code, rather than inline while you construct the plot ? and have the levels be Yes and No as you want them

Using the {palmer penguins} dataset to make a minimal reproducible example, this approach seems to work:

data <- palmerpenguins::penguins %>% 
  mutate(sex_f = factor(sex))

ggplot(data) +
  aes(x = species, y = culmen_length_mm,  color = sex_f) +
  geom_boxplot() +
  geom_point(position = position_jitterdodge()) +
  scale_color_discrete(name = "Sex", labels = c("Female", "Male", "Missing")) 
#> Warning: Removed 2 rows containing non-finite values (stat_boxplot).
#> Warning: Removed 2 rows containing missing values (geom_point).

Created on 2020-07-04 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.