Jorge Martínez
Jorge Martínez

Reputation: 71

How to change the text in the colour legend of ggplot2

I have this code:

ggplot(databoth, aes(withxstep)) + 
       geom_point(aes(y = withnassoc, colour = "withnassoc"), size = 2.8) + 
       geom_point(aes(y = withoutnassoc, colour = "withoutnassoc"), size = 1 ) +
       labs(colour = "Legend") +
       labs(x = "Time") +
       labs(y = "N associations")

How do I modify the withnassoc and the withoutnassoc? I would like it to be "With Activities" and "Without activities".

Upvotes: 1

Views: 47

Answers (1)

M--
M--

Reputation: 28826

This should answer your question:

ggplot(databoth, aes(withxstep)) + 
       geom_point(aes(y = withnassoc, colour = "withnassoc"), size = 2.8) + 
       geom_point(aes(y = withoutnassoc, colour = "withoutnassoc"), size = 1 ) +
              labs(colour = "Legend", x = "Time", y = "N associations") +
              scale_color_manual(values = c("red", "blue"), 
                                 labels = c("With Activities", "Without activities"))

For this example data-set:

exampledata <- structure(list(withxstep = structure(c(4L, 3L, 2L, 1L), 
.Label = c("2017-06-27", "2017-06-28", "2017-06-29", "2017-06-30"), class = "factor"), 
withnassoc = c(1, 2, 3, 4), withoutnassoc = c(5, 6, 7, 8)), .Names = c("withxstep", 
"withnassoc", "withoutnassoc"), class = "data.frame", row.names = c(NA,-4L))

This would be the plot:

enter image description here

Upvotes: 2

Related Questions