r - geom_bar 中的 Scale_fill_brewer

标签 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)]中的错误:“S4”类型的对象不可取子集 另外:警告消息:

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

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

最佳答案

您可能需要转换sp$LWdays显式地转换为因子而不是字符向量。 sp$LWdays <- factor(sp$LWdays) .

关于r - geom_bar 中的 Scale_fill_brewer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22658884/

相关文章:

r - 带有用户输入的 knitr

r - 使用带有 ggplot2 的 directlabels 来标记具有与主层不同的数据的第二层

r - ggmap 中带有自定义颜色代码的 Voronoi 曲面分割?

r - plotly + ggplot2 : Some tooltips missing in stacked barplot

r - 如何删除ggplot2中图例键之间的空格

r - 在 rpart 中使用 minsplit 和不等权重

r - 其中的倒数

R - ggplot geom_dotplot 形状选项

r - 抑制 test_that 输出 "Test passed :)"

删除图例 ggplot 2.2