简体   繁体   English

GGplot scale_fill_manual() 意外行为

[英]GGplot scale_fill_manual() unexpected behaviour

Edit* Assume I've been down-voted due to not being replicable, fair enough though these are often complex/sensitive data sets and I wanted to be cautious.编辑* 假设我因为不可复制而被否决,虽然这些通常是复杂/敏感的数据集,但我想保持谨慎。

That being said I solved this by setting the factor levels of the fill= call in GGplot.话虽如此,我通过在 GGplot 中设置fill=调用的因子水平来解决这个问题。 Still not 100% sure why this works (ie how this affected colour order), will do some research and find out.仍然不是 100% 确定为什么会这样(即这如何影响颜色顺序),将进行一些研究并找出答案。

Having an issue with manually setting colour in a bar plot using scale_fill_manual使用scale_fill_manual在条形图中手动设置颜色时scale_fill_manual

So I feed a list of colours into a bar plotting routine, which pulls data from a code frame (ie scores for a list of names), appends a score at the top and bottom of that list (ie an average and a benchmark), then sets colours to be a rainbow spectrum (depending on number of names) with set colours for the top and bottom bars average and benchmark (blue and gold)因此,我将颜色列表输入到条形绘图例程中,该例程从代码框架中提取数据(即名称列表的分数),在该列表的顶部和底部附加一个分数(即平均值和基准),然后将颜色设置为彩虹光谱(取决于名称的数量),并为顶部和底部条形平均值和基准(蓝色和金色)设置颜色

My issue is that the colours are out of order somehow...我的问题是颜色以某种方式不正常......

plotdata <- subset(groupdata,scale.code==(k)&year!=prior.year) #select data for the given scale
#create the plot for the scale
curr.plot <- ggplot(plotdata, aes(x = FBMA_ID, y = Plot.score, fill=FBMA_ID))+ 
  geom_bar(width=0.7, stat="identity") +  
  theme(legend.position="none") +
  coord_flip() +
  scale_x_discrete(limits = rev(unique(plotdata$FBMA_ID)))+
  scale_y_continuous(limit = MMlimits, oob=squish, minor_breaks = value.breaks.minor, 
                     breaks = value.breaks.major, labels = value.labels, expand = c(0,0)) +
  scale_fill_manual(values=rev(temp.col))+

note that the colours used (ie temp.col ) output like this, in the correct order请注意,使用的颜色(即temp.col )以正确的顺序输出

 [1] "gold"        "#BFFF80"    
 [3] "#9FFF80"     "#80FF80"    
 [5] "#80FF9F"     "#80FFBF"    
 [7] "#80FFDF"     "#80FFFF"    
 [9] "#80DFFF"     "#80BFFF"    
[11] "#809FFF"     "#8080FF"    
[13] "#9F80FF"     "#BF80FF"    
[15] "#DF80FF"     "#FF80FF"    
[17] "#FF80DF"     "#FF80BF"    
[19] "#FF809F"     "#FF8080"    
[21] "#FF9F80"     "#FFBF80"    
[23] "dodgerblue4"

though the plot outputs as this虽然情节输出为

在此处输入图片说明

with the first and last colours now second and second last.用第一个和最后一个颜色现在第二个和倒数第二个。

I've probably missed something glaring, long day!我可能错过了一些引人注目的东西,漫长的一天!

I can do a dput if required.如果需要,我可以做一个 dput。

Cheers, Alex干杯,亚历克斯

Setting the factor levels in the plotdata frame as per below solved this.如下在 plotdata 框中设置因子水平解决了这个问题。 Thanks to Roman Luštrik for the guidance ad contribution.感谢 Roman Luštrik 的指导广告贡献。

ggplot(plotdata, aes(x = FBMA_ID, y = Plot.score, fill=factor(plotdata$FBMA_ID, levels = names))

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

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