英文:
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('right',c('A','B','C'), 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 <- 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 <- if (is.null(density))
c("white", "lightblue", "mistyrose",
"lightcyan", "lavender", "cornsilk")
else par("fg")
因此,你可以在调用legend
时自己使用相同的颜色:
pie(table(iris$Species))
legend('right', c('A','B','C'), fill = c("white", "lightblue", "mistyrose"))
英文:
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 <- if (is.null(density))
c("white", "lightblue", "mistyrose",
"lightcyan", "lavender", "cornsilk")
else par("fg")
So, you can use the same colors yourself in the call to legend
pie(table(iris$Species))
legend('right', c('A','B','C'), fill = c("white", "lightblue", "mistyrose"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论