Thank you, but then I will have long labels also on the axis, 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, <ruipbarra...@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@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 > [[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.