r - 在scale_*_manual中使用palette参数

标签 r ggplot2

背景

?scale_fill_manual 的帮助说:

 ...: Arguments passed on to ‘discrete_scale’
          palette A palette function that when called with a single
              integer argument (the number of levels in the scale)
              returns the values that they should take.
<小时/>

代码

因此,我尝试了:

library(ggplot)
library(dplyr)

my_pal <- colorRampPalette(1:3)

(p <- ggplot(mtcars %>% 
              group_by(cyl) %>% 
              summarise(mpg = mean(mpg)), 
             aes(x = factor(cyl), y = mpg, fill = factor(cyl))) +
   geom_col())
<小时/>

问题

但是如果我想将调色板功能添加到比例中,它就不起作用:

p + scale_fill_manual(palette = my_pal)
# Error in as.vector(x, "character") : 
#   cannot coerce type 'closure' to vector of type 'character'
<小时/>

问题

如何正确使用palette参数?我知道如何正确设置颜色,但我想了解 palette 参数的用途以及如何使用它。如果它是一个内部参数,我想知道为什么它首先在 scale_*_manual API 中可用。

最佳答案

这似乎是一个错误。请参阅(已关闭)问题:scale_colour_manual(palette=foo): cannot coerce type 'closure' to vector of type 'character' :

@sjackman:

How is the palette argument meant to be used; is there an example in the documentation?

@克劳斯威尔克:

It's not meant to be used at all. The bug is that palette is accessible and mentioned in the documentation.

关于r - 在scale_*_manual中使用palette参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56556842/

相关文章:

r - 将多面 ggplots (facet_wrap) 与 R 中的牛图对齐

R:连续 x 轴上的条形图(时间刻度)

r - Bootstrapping 比较两组

r - 预测 v7 和 ggplot2 图形将拟合线添加到自动绘图

r - R中的聚类分析期间"NAs introduced by coercion"

r - ggplot2:使用带有 facet_grid 的 geom_dotplot 时没有自由轴比例

r - 按组合并行,每行具有不同的 NA

RSelenium findElement 适用于 firefox :2. 53.1 但不适用于 firefox:latest

r - 图像作为图表中的标签?

r - ggplot : legend for emojis in plot