Dear Luigi, ggplot2 will use the factor levels as default values for the legend labels. Setting the factor labels of your target values fixes your problem.
Best regards, ir. Thierry Onkelinx Statisticus / Statistician Vlaamse Overheid / Government of Flanders INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance thierry.onkel...@inbo.be Havenlaan 88 bus 73, 1000 Brussel *Postadres:* Koning Albert II-laan 15 bus 186, 1210 Brussel *Poststukken die naar dit adres worden gestuurd, worden ingescand en digitaal aan de geadresseerde bezorgd. Zo kan de Vlaamse overheid haar dossiers volledig digitaal behandelen. Poststukken met de vermelding ‘vertrouwelijk’ worden niet ingescand, maar ongeopend aan de geadresseerde bezorgd.* www.inbo.be /////////////////////////////////////////////////////////////////////////////////////////// To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey /////////////////////////////////////////////////////////////////////////////////////////// <https://www.inbo.be> Op wo 12 mrt 2025 om 11:36 schreef Luigi Marongiu <marongiu.lu...@gmail.com >: > 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@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.