R: 用于饼图的默认调色板

huangapple go评论67阅读模式
英文:

R: default palette used for pie charts

问题

The code you provided is in English, and you're asking for the translation of code-related parts. Here's the translated code:

我想要在 `R` 中的饼图上添加图例
默认颜色调色板对我来说很好,但我如何在图例中使用它(以匹配图表的颜色)?

pie(table(iris$Species))
legend('right',c('A','B','C'), fill = colors(3))

(我知道我可以提前创建一个用于图表中的图例的颜色向量,就像 `my_colors <- colors(3)`,但这将不再是默认颜色。
官方文档仅说明,如果省略 `col` 参数,则 `pie()` 函数使用“淡彩色”,但未指定确切的值。)
英文:

I would like to add a legend to a pie chart in R
The default color palette is fine for me, but how can I use it in the legend (to match the colors of the plot)?

pie(table(iris$Species))
legend(&#39;right&#39;,c(&#39;A&#39;,&#39;B&#39;,&#39;C&#39;), fill = colors(3))

(I know that I could create a vector of colors to use in the chart in the legend in advance like in my_colors &lt;- colors(3), but this would then not be the default colors.
The official documentation only says that the pie() function uses "pastel" colors if the col argument is omitted, but doesn't specify the exact values.)

答案1

得分: 3

如果你查看pie函数的主体部分,你会看到颜色在以下行中硬编码在其中:

if (is.null(col)) 
    col &lt;- if (is.null(density)) 
        c(&quot;white&quot;, &quot;lightblue&quot;, &quot;mistyrose&quot;, 
            &quot;lightcyan&quot;, &quot;lavender&quot;, &quot;cornsilk&quot;)
    else par(&quot;fg&quot;)

因此,你可以在调用legend时自己使用相同的颜色:

pie(table(iris$Species))
legend(&#39;right&#39;, c(&#39;A&#39;,&#39;B&#39;,&#39;C&#39;), fill =  c(&quot;white&quot;, &quot;lightblue&quot;, &quot;mistyrose&quot;))

R: 用于饼图的默认调色板

英文:

If you look at the body of the pie function, you will see that the colors are hard-coded into it in the following lines:

   if (is.null(col)) 
        col &lt;- if (is.null(density)) 
            c(&quot;white&quot;, &quot;lightblue&quot;, &quot;mistyrose&quot;, 
                &quot;lightcyan&quot;, &quot;lavender&quot;, &quot;cornsilk&quot;)
        else par(&quot;fg&quot;)

So, you can use the same colors yourself in the call to legend

pie(table(iris$Species))
legend(&#39;right&#39;, c(&#39;A&#39;,&#39;B&#39;,&#39;C&#39;), fill =  c(&quot;white&quot;, &quot;lightblue&quot;, &quot;mistyrose&quot;))

R: 用于饼图的默认调色板

huangapple
  • 本文由 发表于 2023年2月6日 04:01:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定