我正在尝试在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,它会很好地工作,但是如果我使用它,我会得到:
object [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/