[R] How to customize legend labels in ggplot2?

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Mar 13 08:16:09 CET 2025


Hello,

Inline.

Às 05:19 de 13/03/2025, Luigi Marongiu escreveu:
> Thank you, but then I will have long labels also on the axis, 


I am not getting the same legend labels and axis labels, only the legend 
is labelled as variable lbls, the axis labels are numeric.
I am running the following reproducible example.


library(ggplot2)

lbls <- c("Condition 1", "Condition 2", "Condition 3", "Control")
COLS <- 1:4

df = data.frame(Target = 1:4,
                 Rate = c(0.02078663, 0.03685543, 0.02238002, 0.05033979),
                 SD = c(0.003043398, 0.001447410, 0.002998729, 0.002171813))

df$Target = factor(df$Target)

ggplot(df, aes(x = Target, y = Rate, colour = Target, group = Target)) +
   geom_point(size = 8) +
   geom_errorbar(aes(ymin = Rate - SD, ymax = Rate + SD), width = 0.1) +
   # this is the only thing different from your original code
   scale_colour_manual(values = COLS, labels = lbls) +
   xlab(expression(bold("Class"))) +
   ylab(expression(bold("Value"))) +
   theme_classic(base_size = 15)


What code are you running? The OP code?

Hope this helps,

Rui Barradas

making the
> plot too crowded. I would like instead to force the long labels only on the
> legend...
> 
> On Wed, 12 Mar 2025, 12:47 Rui Barradas, <ruipbarradas using sapo.pt> wrote:
> 
>> Às 10:35 de 12/03/2025, Luigi Marongiu escreveu:
>>> I have a data frame with measurements in different conditions. I set
>>> the conditions as a factor using a notation for ease of use. I now
>>> want to plot the data and assign meaningful labels to the factors. I
>>> am using ggplot2; for the x axis I would like to keep the factors but
>>> in the legend I would like to use custom values.
>>> I tried different combinations but none worked.
>>> What is the correct way to assign
>>> custom labels to legends in ggplot2?
>>> Thank you
>>>
>>> EXAMPLE:
>>> ```
>>> df = data.frame(Target = 1:4,
>>>                   Rate = c(0.02078663, 0.03685543, 0.02238002,
>> 0.05033979),
>>>                   SD = c(0.003043398, 0.001447410, 0.002998729,
>> 0.002171813))
>>> df$Target = factor(df$Target)
>>> ggplot(df, aes(x=Target, y=Rate, colour=Target, group=Target)) +
>>>     geom_point(size=8) +
>>>     geom_errorbar(aes(ymin=Rate-SD, ymax=Rate+SD), width=.1) +
>>>     scale_colour_manual(values = COLS) +
>>>     xlab(expression(bold("Class"))) +
>>>     ylab(expression(bold("Value"))) +
>>>     theme_classic(base_size = 15)
>>> ```
>>> NOTE: if using
>>> ```
>>> ...
>>>     theme_classic(base_size = 15, labels = c("Condition 1", "Condition 2",
>>>                                              "Condition 3", "Control"))
>>> ```
>>> I get the error:
>>>
>>> Error in theme_classic(base_size = 15, labels = c("Condition 1",
>>> "Condition 2",  :
>>>     unused argument (labels = c("Condition 1", "Condition 2", "Condition
>>> 3", "Control"))
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>> https://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>> Hello,
>>
>> The colors COLS are missing from the question.
>> As for the labels, use
>>
>>
>> lbls <- c("Condition 1", "Condition 2", "Condition 3", "Control")
>>
>> and then
>>
>> scale_colour_manual(values = COLS, labels = lbls)
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>> --
>> Este e-mail foi analisado pelo software antivírus AVG para verificar a
>> presença de vírus.
>> www.avg.com
>>
> 


-- 
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus.
www.avg.com



More information about the R-help mailing list