jsguy
jsguy

Reputation: 2179

R ggplot2: changing the legend title and labels in this example dataset

n   value   name
1   20  1
2   30  1
3   25  1
1   40  2
2   12  2
3   39  2

This is how I plot it now:

require(ggplot2)
data <- read.table("test", sep = "\t", header = TRUE,)
ggplot(data, aes(n, value,color=as.character(name))) + 
geom_point(aes(n,value)) + geom_line(aes(n,value))
dev.off()

enter image description here I would like to change "as.character(name)" to "New Title" and the values "1" and "2" to "value1" and "value2".

I tried the following but it didn't work:

require(ggplot2)
data <- read.table("test", sep = "\t", header = TRUE,)
ggplot(data, aes(n, value,color=as.character(name))) + geom_point(aes(n,value)) 
+ geom_line(aes(n,value)) +
 scale_fill_manual(name="My title", values=c("value1", "value2"))
dev.off()

Upvotes: 0

Views: 394

Answers (1)

neilfws
neilfws

Reputation: 33772

You need scale_color_manual not scale_fill_manual. Then values refers to color values, which you must supply, and the third parameter is labels.

+ scale_color_manual(name = "New Title", 
                     labels = c("value1", "value2"), 
                     values = c("red", "green"))

Upvotes: 1

Related Questions