How do I manually change the key labels in a legend in ggplot2
Asked Answered
N

3

45

I am preparing a plot for publication. I created a stacked box plot to show frequency of patients in each group who were some complicated accumulation of seronegatives versus not. The legend is using the labels from the data frame which are appropriate for us who are working on the project but no for publication. I want to change the names to something more rapidly understood by the reader.

So for instance run the following script

grp <- gl(n=4,k=20,labels=c("group a","group b","group c", "group d"))
value <- runif(n=80, min=10, max=150)
outcome <- cut(value,2)
data <- data.frame(grp,value,outcome)
ggplot(data, aes(grp, fill=outcome)) + geom_bar() +xlab("group") 
             +ylab("number of subjects") + labs(fill="Serologic response")

That code creates key labels "(10.4,80]" and "(80,150]" which are not suitable for publication. Instead I would want "double negative" and "positive for a and/or b".

I guess I could go back to the dataframe and transform to get a new variable with the correct labeling. Or I could just relabel my factor? However, I would prefer to do it at the time of plotting.

Newbold answered 6/9, 2011 at 16:31 Comment(0)
F
46

The standard way is to use the scale functions to change the displayed labels for groups. You can replace your ggplot call with

ggplot(data, aes(grp, fill=outcome)) + geom_bar() +xlab("group") +
  ylab("number of subjects") + 
  scale_fill_discrete("Serologic response", 
                      breaks=c("(10.1,79.9]","(79.9,150]"), 
                      labels=c("double negative", "positive for a and/or b"))

Note that the scale's title has been incorporated into the scale_fill_discrete call. You can do this with the axes too, if you like

ggplot(data, aes(grp, fill=outcome)) + geom_bar() +
  scale_x_discrete("group") +
  scale_y_continuous("number of subjects") + 
  scale_fill_discrete("Serologic response", 
                      breaks=c("(10.1,79.9]","(79.9,150]"), 
                      labels=c("double negative", "positive for a and/or b"))
Farias answered 6/9, 2011 at 18:39 Comment(0)
N
30

I found a hybrid way of doing it. It does relabel the factor but I do not have to do it in the dataframe. Instead I just do it in the ggplot command.

ggplot(data, aes(grp, fill=factor(outcome,labels=c("low","high")))) + 
  geom_bar() +xlab("group") +ylab("number of subjects") +
   labs(fill="Serologic response")

Are there any other ways?

Newbold answered 6/9, 2011 at 17:41 Comment(1)
This should be the right answer specially if you are using scale_x_continuous()Penology
R
0

In ggplot2, you can use scale_fill_manual()

colors <- c("red","blue")
outcome_labels <- c("low","high")

ggplot(data, aes(grp, fill=outcome)) + 
  geom_bar() +
  scale_fill_manual(values = colors,labels = outcome_labels) +
  xlab("group") +ylab("number of subjects") + labs(fill="Serologic response")

How the plot looks like

enter image description here

Rains answered 27/3, 2024 at 13:10 Comment(2)
Please reread the question carefully and adjust the legend labels accordingly. Your labels are not what the OP requested. Thanks.Equivocation
The OP has two factor outcomes. The labels that I provided is for outcome labels. So, updated the variable name accordingly. ThanksRains

© 2022 - 2025 — McMap. All rights reserved.