在ggplot boxplot中标记图例

时间:2014-03-08 03:12:35

标签: r ggplot2

我有以下代码:

legendCat<-c('Chemical','Computers & Communications','Drugs & Medical',
  'Electronic & Electrical','Mechanical','Others')
ggplot(cleaned,aes(x=CAT,y=ORIGINAL, fill=as.factor(CAT)))+geom_boxplot()+
  guides(colour=guide_legend(override.aes=list(legendCat)))

但是图例的标签并不是根据我定义的legendCat向量来实现的。我怎么做?
获得的情节: enter image description here

提前致谢

1 个答案:

答案 0 :(得分:0)

图例中不需要特殊的矢量。只需使用:

ggplot(cleaned,aes(x=CAT,y=ORIGINAL, fill=as.factor(CAT))) +
  geom_boxplot() +
  scale_fill_discrete(breaks = 1:6, labels = c('Chemical','Computers & Communications','Drugs & Medical','Electronic & Electrical','Mechanical','Others'))