geom_bar中的Scale_fill_brewer

时间:2014-03-26 10:59:48

标签: r ggplot2

我正在尝试在barplot(geom_bar)中使用scale_fill_brewer,但我总是遇到同样的错误,我不明白。 在这里,那将是我的代码:

LW25_cwt <-data.frame(
    "name"=c("N","NE","E","SE","S","SW","W","NW","C","A","U"),
    "days/winter"=c(7.07,3.6,8.4,5.5,10.5,6.2,7,4,54,43,10)) 
LW10_cwt <-data.frame(
    "name"=c("N","NE","E","SE","S","SW","W","NW","C","A","U"),
    "days/winter"=c(2.07,1.6,3.4,6.5,11,6.7,7.2,10,51,42,4))


  LW25_cwt$LWdays <- "LW25"
   LW10_cwt$LWdays <- "LW10"
   sp=rbind(LW25_cwt,LW10_cwt)


   c2 <- ggplot(sp, aes(y=days.winter,x=name, fill=LWdays)) + 
        ggtitle("LoW-Wind days (1961-2000)") + 
        theme(plot.title = element_text(lineheight=1,face="bold", color="black",
        size=17,vjust=2.5))+     
        ylab('days/year') +  xlab('CWTs Types') +
        geom_bar(stat="identity", position="dodge") 
   c2 <- c2 + scale_fill_brewer(palette="Set2")

如果我不使用scale_fill_brewer,它完全有效,但如果我使用它,我就有这个:

对象[seq_len(ile)]中的错误:对象类型&#39; S4&#39;不是子集 另外:警告信息:

1: In class(y) <- class(x) :
  Setting class(x) to multiple strings ("brewer", "discrete", ...); result will no longer be an S4 object
2: In class(y) <- class(x) :
  Setting class(x) to multiple strings ("brewer", "discrete", ...); result will no longer be an S4 object

任何想法?我一直在寻找它,但不幸的是我没有找到解决方案。

1 个答案:

答案 0 :(得分:0)

您可能必须明确地将sp$LWdays转换为因子而不是字符向量。 sp$LWdays <- factor(sp$LWdays)