第一次执行代码时颜色不正确 [英] Colors incorrect on first execution of code

查看:107
本文介绍了第一次执行代码时颜色不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码可以在一页上绘制多个堆叠的条形图。我指定一个调色板,以便每个数据组在每个图表中都与相同的颜色关联。我发现第一个图表中的颜色不正确,但随后的所有图表中的颜色均正确。如果我在同一R会话中重新运行该代码,则所有图表均具有正确的颜色。但是,如果我退出R并运行代码,则第一个图表的颜色也会再次错误。

I have a code that plots several stacked bar charts on one page. I specify a color palette so each data group is associated with the same color in every chart. I have found that the colors are incorrect in the first chart, but correct in all subsequent charts. If I rerun the code within the same R session, all the charts have the correct colors. However, if I exit R and run the code, the first chart again has the wrong colors.

我的代码非常复杂,因此我整理了一个简单的示例来说明问题。同样,在开始新的R会话后第一次运行代码时,颜色不正确。如果代码在同一R会话中重新运行,则它们是正确的。

My code is pretty complex, so I put together simple example that illustrates the problem. Again, the first time I run the code after starting a new R session, the colors are incorrect. They are correct if the code is rerun within the same R session.

# Data vector for bar plot,
#
dvec        <- c(   0,     0,    0,    0,     0,     0,
                    0,     0,    0,    0,     0,     0,
                    0,     0,    0,    0,     0,     0,
                 1.73,     0,    0,    0,     0,     0,
                 4.81,     0,    0, 0.03,  0.03,     0,
                 2.77,     0,    0, 0.42,  1.04,     0,
                 2.84,  0.02,    0, 2.25,  4.21,  0.02,
                 1.32,  0.54, 0.01, 3.44, 10.49,  0.01,
                 0.13,  1.63, 0.02, 4.42, 17.70,  0.03,
                 0.08,  2.49, 0.03, 8.64, 31.07,  0.02,  
                    0,  3.08, 0.05, 9.04, 50.22,  0.01)

# matrix to pass to barplot, 12 columns, 6 rows

smat <- matrix(dvec,nrow=6)                    

# use this color palette and plot the barchart

zoopal <- palette(c("magenta","red","darkred","yellow","cyan","blue"))

barplot(smat,space=0,col=zoopal,axes=FALSE,axisnames=FALSE,
                  xaxs="i",yaxs="i",ann=FALSE)    

我在启动R后第一次运行代码,得到的是:

The first time I run the code after starting R, I get this:

再次运行代码时,将获得正确的颜色:

When I run the code again, I get this with the correct colors:

我不明白为什么会这样。几乎就像我第一次运行代码时没有使用我指定的调色板。我应该注意,尽管我在MacOSX 10.8.5下使用R版本3.2.1开发了R代码,但是当代码在同事的Windows计算机上运行时,也会发生同样的事情。谁能解释我在做什么错?谢谢。

I don't understand why this happens. It is almost as if the palette I specify isn't being used the first time the code is run. I should note that although I developed the R code using R version 3.2.1 under MacOSX 10.8.5, the same thing happens when the code is run on a colleague's Windows computer. Can anyone explain what I'm doing wrong? Thanks.

推荐答案

我可以在当前版本的R和MacOS上进行复制。似乎与 palette 的(不必要的)调用有关。忽略该调用可以解决问题。请注意,实际上这就是?palette 的预期结果,它说:一个字符向量,给出有效的 调色板。 (强调)

I can reproduce on a current version of R and MacOS. It seems to have something to do with the (unnecessary) call to palette. Omitting that call cures the problem. Note that is actually what is described as the expected result from ?palette which says: "A character vector giving the palette which was in effect. " (Emphasis added)

在我看来,这确实是调色板的一种非功能性行为。显然,它被设计为系统的副作用,而不是返回当前有用的值,并且返回的值可能被假定存储为 oldpal或类似的值。如帮助页面上所讨论的,应该使用调色板将(后续)数字到颜色的映射而不是设置,而不是返回颜色列表。

This does seem to me to be a rather non-functional behavior of palette. It is apparently designed to act as a system side-effect rather than returning a currently useful value and the value that is returned is presumably supposed to be stored as "oldpal" or some such. As discussed on the help page, the use of palette is supposed to change the mapping of (subsequent) numbers to colors rather than setting, rather than returning a list of colors.

这篇关于第一次执行代码时颜色不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆