首页 > 解决方案 > 在 scale_continuous ggplot2 中使用手动功能

问题描述

此代码在没有 scale_y_continuous geom 的情况下完美运行。为什么当我尝试在 scale_y_continuous geom 中使用手动函数时,此代码会返回错误:

library('tibble')
library("ggplot2")

df <- tibble(x=c(1:5),
        y=c(11:15))

MyFunction <- function(x){
 if(x>12){
   return(paste0('$', x))
 }else{
   return(paste0('&', x))
 }
}

ggplot(df, aes(x,y))+
geom_line()+
scale_y_continuous(
 labels = function(x) MyFunction(x)
)

错误文字:

 Warning message:
 In if (x > 12) { :
 the condition has length > 1 and only the first element will be used

标签: rggplot2

解决方案


推荐阅读