首页 > 解决方案 > 自定义填充渐变在 self$oob(x, range = limits) 中产生错误:尝试应用非函数

问题描述

我正在尝试使用ggplot自定义填充渐变创建一个绘图。我想设置填充渐变的限制,并在 oob 上使用 squish 参数来设置填充渐变的限制,但我最终得到了错误:Error in self$oob(x, range = limits) : attempt to apply non-function.该错误应该能够使用下面的代码重现:

# Libraries
library(ggplot2)
library(scales)

# Create data frame
df <- data.frame("name" = c("A", "B", "C", "D", "E", "F", "G", "H"),
                  "percent" = c(0, 5, 10, 11, 22, 25, 30, 80),
                  "differential" = c(-20, -10, -5, 5, 10, 22, 30, 50))
# Plot
p <- ggplot(df, aes(percent, differential, fill = differential))+
    geom_point(shape = 21, colour = "black")+
    scale_fill_gradient2(low = "#C93135",
                        high = "#1375B7",
                        mid = "grey90",
                        midpoint = 0,
                        aesthetics = "fill",
                        limits = c(-30,30),
                        oob = squish(x=df$differential,
                                     range=c(-30,30)))
p

任何帮助将不胜感激!先感谢您。

标签: rggplot2

解决方案


正如 P.Chakytei 所指出的,我只需要在oob = squish没有所有额外参数的情况下使用。


推荐阅读