2

I have a question regarding variable names in a legend from ggplot2

I believe that you find it simple, so I don´t provide reproducible code for now.

I run this ggplot2 query on long data:

ggplot(subset(BaumA_long, 
              Messpunkt %in% c("Tleaf","Tair","Tcrown","TbarkS","TbarkN", "Tsoil")),
       aes(x=Zeit, y=value, color=Messpunkt)) +
  geom_line(size=0.3) +
  scale_x_datetime(limits=start.endKW26, labels = date_format("%d.%m. %H h",tz = "CET"), 
                   breaks = date_breaks("12 hours"), expand = c(0, 0)) +
  scale_y_continuous(limits = c(5, 55), breaks = seq(10, 55, by = 3)) +
  labs(x="Datum", y="Temperatur") +
  ggtitle("Cbet 1")+
  theme(plot.title = element_text(vjust=-15),
        axis.text.x = element_text(angle = 35))+
  theme(legend.position='top') +
  #labels=c("Tleaf"="Blatt", "Tair"="Luft", "Tcrown"="Krone", 
  #         "TbarkS"="Borke_S", "TbarkN"="Borke_N","Tsoil"="Substrat") +
  scale_color_manual(values = c("Tleaf" = "#339900", "Tair" = "blue", "Tcrown"= "red", 
                                "TbarkS" = "orange2", "TbarkN" = "pink", "Tsoil" = "brown"))

this gives me the following plot: png of final plot

All I want to change is the names of the variables "Tleaf" and so on in the legend. I tried with:

ggplot + 
  labels=c("Tleaf"="Blatt", "Tair"="Luft", "Tcrown"="Krone", 
           "TbarkS"="Borke_S", "TbarkN"="Borke_N","Tsoil"="Substrat") +

with no success.

For keeping my code clean and short I want to avoid renaming all the variables, because I have many datasets.

1 Answer 1

2

Use scale_color_discrete():

Code

library(ggplot2)

labels <- c(var1 = "Custom Value 1", var2 = "Custom Value 2", var3 = "Custom Value 3")

ggplot(df, aes(x, val, col = var)) +
  geom_line() +
  scale_color_discrete(labels = labels)

Data

structure(list(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L), var = c("var1", "var1", "var1", "var1", 
"var1", "var1", "var1", "var1", "var1", "var1", "var2", "var2", 
"var2", "var2", "var2", "var2", "var2", "var2", "var2", "var2", 
"var3", "var3", "var3", "var3", "var3", "var3", "var3", "var3", 
"var3", "var3"), val = c(-0.33, 0.96, 1.39, 0.92, 1.06, -0.63, 
-0.33, 2.94, -1.42, -0.15, 3.6, 1.59, 3.87, 3.25, 2.25, 2.83, 
2.71, 1.13, 4.49, 0.8, 2.67, 3.22, 3.23, 3.39, 2.92, 3.04, 3, 
3.3, 3.22, 3.4)), class = "data.frame", row.names = c(NA, -30L
))
Sign up to request clarification or add additional context in comments.

2 Comments

sorry that I respond so late. The Solution doesn´t work, becaus I cannot Use Scale_color_discrete and scale_color_manual at the same time. I get the error:(Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.)
scale_color_manual also has a labels argument which you can use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.