【问题标题】:eval() not working with ggplot, returns "non-numeric argument to binary operator"eval() 不适用于 ggplot,返回“二进制运算符的非数字参数”
【发布时间】:2018-11-19 19:54:22
【问题描述】:

我将以下 ggplot 元素存储为 text

eval(parse(text = " annotate('text', as.Date('2013-12-31'), 30.31049879  * 1.02,
       label = paste0( 19.48 , percent( 0.055 )), color = 'Blue') +
       annotate('text', as.Date('2013-12-31'), 33.341548669  * 1.02,
       label = paste0( 21.43 , percent( 0.048 )), color = 'Blue') "))

eval() 返回错误“二进制运算符的非数字参数”,因为它试图将两个部分相加,但加号与 ggplot 的使用方式不同。有什么办法可以避免这个问题吗?

【问题讨论】:

  • 你用parse函数做什么?既然您对所有内容都进行了硬编码,那么简单地使用annotate() 会不会更容易。
  • @Jrakru56 我有大约 20 个这样的注释,所以我发现使用循环添加它们可能更容易,而不是硬编码 20 行。
  • 如果是这种情况,那么您最好创建一个data.framelist 来保存您的xy 和'label
  • 类似这样的东西:dt<- mtcars; ggplot(data=dt) + geom_point(aes(x=mpg, y =hp )) + geom_text(aes(label = rownames(dt), x =mpg, y = hp))

标签: r ggplot2


【解决方案1】:

通过将ggplot() + 放在代码开头来解决这个问题,即

eval(parse(text = "ggplot() +
annotate('text', as.Date('2013-12-31'), 30.31049879  * 1.02,
           label = paste0( 19.48 , percent( 0.055 )), color = 'Blue') +
               annotate('text', as.Date('2013-12-31'), 33.341548669  * 1.02,
               label = paste0( 21.43 , percent( 0.048 )), color = 'Blue') "))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多