How to create a legend using the function "legend" if my labels are hour data?

Thanks for adding the reprex but have in minde that you are not using a copy/paste friendly format and your reprex is not self contained making it harder to help you, in case you prefer here is the spanish version of the reprex guide

About your question, you just have to use a POSIXct value for the x axis, see this example.

temp_peces <- data.frame(stringsAsFactors=FALSE,
                         Hora = as.POSIXct(c("1899-12-31 00:00:00", "1899-12-31 00:30:00",
                                  "1899-12-31 01:00:00", "1899-12-31 01:30:00",
                                  "1899-12-31 02:00:00", "1899-12-31 02:30:00")),
                         Temperatura = c(18, 18.5, 18, 18, 18, 18),
                         Condición = as.factor(c("Estable", "Estable", "Estable", "Other Value", "Other Value",
                                                 "Other Value"))
)

plot(temp_peces$Hora,
     temp_peces$Temperatura,
     col = temp_peces$Condición,
     pch = 19,
     main = "Temperatura diaria",
     xlab = "Hora",
     ylab = "Temperatura (°C)",
     cex = 2)

legend(x = as.POSIXct("1899-12-31 01:30:00"),
       y = 18.4,
       legend = levels(temp_peces$Condición),
       col = c(1:3),
       pch = 19)

1 Like