1

如何为 geom_area 中的图例添加制服?我尝试了一些东西,但它不起作用。

    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
4

1 回答 1

2

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])

enter image description here

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")  
于 2013-09-16T10:28:04.287 回答