Stata_user
Stata_user

Reputation: 564

Changing the legend title on a graph

I am trying to change the legend title on a graph. If I use scale_fill_discrete() to change the legend title (See below), I get something strange where the legend shows up twice (see graph below). I only want it to show up one time and with the text "SES" and "Low" and "High" as values (just like the bottom legend on the graph now).

What am I doing wrong? Thanks for your help.

    # graphs
    plots[[j]] <-ggplot(cdat, aes(x = whr , y=perc, group=factor(ses3), color=factor(ses3), fill=factor(ses3))) +
    theme_bw() +
    geom_area(alpha=0.5) +

    scale_fill_discrete(name="SES",
                        breaks = c("0", "1"),
                        labels=c("Low", "High"))+

    theme(plot.title = element_text(hjust = 0, size=10, face="bold")) +
    labs(y="Density", x = "Treatment hrs (in 3 hr units)")+
    theme(text = element_text(size = 10, hjust=0.5))

enter image description here

Upvotes: 1

Views: 77

Answers (1)

thothal
thothal

Reputation: 20329

This is behaviour is due to the fact that you map bth color and fill to factor(ses3) but provide only a named scale for fill adding

scale_color_discrete(name="SES",
                     breaks = c("0", "1"),
                     labels=c("Low", "High"))

should do the trick (untested, because you did not provide an reprex)

Upvotes: 1

Related Questions