Reputation: 83
How to add livery for the legend in geom_area? I tried something but it does not work.
time<-as.POSIXlt(c("2013-07-01","2013-07-01","2013-07-02","2013-07-02"),origin = "1960-01-01",tz="GMT")
data<-data.frame(xAxis=time,yAxis=c(3,2,1,2),split=factor(c(1,2,1,2)))
p<-ggplot(data,aes(x=xAxis,y=yAxis,fill=split))
p<-p + geom_area(stat="identity")
#p <- p + scale_color_discrete(name ="Name", labels=LETTERS[1:2])
p <- p + xlab("x-Axis") + ylab("y-Axis")
p
Upvotes: 1
Views: 920
Reputation: 67778
I think you need a scale
that better matches your aes
in ggplot
ggplot(data, aes(x = xAxis, y = yAxis, fill = split)) +
geom_area(stat = "identity") +
scale_fill_discrete(name = "Name", labels = LETTERS[1:2])
If you are going to use 'split' repeatedly and always want to have the same labels, you might consider re-labelling the factor before you start plotting (or whenever informative labels of a factor is relevant, e.g. modelling).
data$split2 <- factor(data$split, labels = LETTERS[1:2])
# no need for the 'labels' argument in scale
ggplot(data, aes(x = xAxis, y = yAxis, fill = split2)) +
geom_area(stat = "identity") +
scale_fill_discrete(name = "Name")
Upvotes: 2