简体   繁体   English

ggplot2中geom_bar上的边框颜色错误

[英]Wrong border colors on geom_bar plot in ggplot2

I am plotting a bar chart of counts of fish in various length categories by sampling week. 我正在通过抽样周绘制各种长度类别的鱼类数量的条形图。
Here is a bit of the database called Chin: 这里有一个名为Chin的数据库:

Trib    Week   Size2  
MM      21     90
SM      21     80
SM      22     100
NM      22     8p

I have converted Size2 to a factor called Fork , and week to a factor called period . 我已将Size2转换为一个名为Fork的因子,将周转换为一个名为period的因子。

Here is my code: 这是我的代码:

period<-as.factor(Chin$week)
Chin1<-cbind(Chin, period)
Fork<-as.factor(Chin$Size2)

g <- ggplot(Chin1, aes(period))
g + geom_bar(aes(fill=Fork))+ theme(axis.text.x = element_text(angle=65, 
 vjust=0.6)) +  theme_bw() +scale_fill_manual(values=c("white","gray90", 
"gray82", "gray61", "gray48", "black"))

This works great except there is no border around the bars or the elements of the legend: 这很好用,除了条形图或图例元素周围没有边框:

没有边框的图表

But when I add colour="black" in this line to get black borders: 但是当我在此行中添加colour="black"以获得黑色边框时:

g + geom_bar(aes(fill=Fork, colour="black"))+ theme(axis.text.x 
element_text(angle=65, vjust=0.6))....

I instead get red borders with a new legend with an item called "black" I get this no matter what color I choose. 我改为使用一个名为"black"的项目获得带有新图例的红色边框。无论我选择何种颜色,我都会得到这个。 The name of the legend item changes (ie blue, green) but the border color stays red and the legend item fill is still black. 图例项目的名称会更改(即蓝色,绿色),但边框颜色保持红色,图例项目填充仍为黑色。 See below. 见下文。 I know this must be a simple silly mistake, but I cannot figure it out. 我知道这一定是一个简单的愚蠢错误,但我无法弄清楚。 Any ideas? 有任何想法吗? Thanks in advance! 提前致谢!

带有红色边框的图表

Move the colour="black" outside the aes() statement. 在aes()语句之外移动colour="black" aes() uses variables from your data to create aesthetics, so you're saying to base color off of the column "black", which doesn't exist. aes()使用数据中的变量来创建美学,所以你要说的是从“黑色”列中获取颜色,而不存在。

Also found here 也在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM