hhh
hhh

Reputation: 52860

How to modify ggplot2 legend_text?

I use

geom_point(aes(colour = factor(ClusterID))) 

but I want to get ClusterID visible without the word factor.

How can I modify the legend title in ggplot2?

enter image description here

Upvotes: 1

Views: 71

Answers (1)

neilfws
neilfws

Reputation: 33802

Better to do the factor conversion before plotting. For example using dplyr, assuming data frame mydata and variables x, y:

library(dplyr)
mydata %>%
  mutate(ClusterID = factor(ClusterID)) %>%
  ggplot(aes(x, y)) + geom_point(aes(color = ClusterID))

Another option is to name the legend in scale_color_discrete:

ggplot(mydata, aes(x, y)) + 
  geom_point(aes(color = factor(ClusterID))) + 
  scale_color_discrete(name = "ClusterID")

Upvotes: 1

Related Questions