【问题标题】:dplyr object not found idplyr 对象未找到我
【发布时间】:2021-11-19 19:19:51
【问题描述】:

我正在尝试在我的图表上添加线条以显示 mean + sd,但我不断收到相同的错误代码:

“平均值错误(NoHealthInsurance):对象'NoHealthInsurance'不是 找到”。

我的括号是不是放错地方了?

hist <- ggplot(data = west) +
  geom_histogram(aes(NoHealthInsurance), 
                 color = wes_palette("Zissou1", 1), 
                 fill = wes_palette("Zissou1", 2)[2]) +
  geom_vline(xintercept = (mean(NoHealthInsurance))) +
  geom_vline(xintercept = (mean(NoHealthInsurance) - sd(NoHealthInsurance)), color = "blue") +
  geom_vline(xintercept = (mean(NoHealthInsurance) + sd(NoHealthInsurance)), color = "blue") +
  geom_vline(xintercept = (mean(NoHealthInsurance) - 2*sd(NoHealthInsurance)), color = "red", linetype = "dotted") +
  geom_vline(xintercept = (mean(NoHealthInsurance) + 2*sd(NoHealthInsurance)), color = "red", linetype = "dotted")

【问题讨论】:

标签: r dplyr error-handling


【解决方案1】:

不确定这是否适用于您的数据,因为您没有提供任何示例数据,但下面的代码适用于 mtcars。基本上,您需要在mean()sd() 函数中添加对数据的引用,例如.data$variable。见下文。

library(ggplot2)

ggplot(data = mtcars) +
  geom_histogram(aes(mpg)) +
  geom_vline(xintercept = (mean(mtcars$mpg))) +
  geom_vline(xintercept = (mean(mtcars$mpg) - sd(mtcars$mpg)), color = "blue") +
  geom_vline(xintercept = (mean(mtcars$mpg) + sd(mtcars$mpg)), color = "blue") +
  geom_vline(xintercept = (mean(mtcars$mpg) - 2*sd(mtcars$mpg)), color = "red", linetype = "dotted") +
  geom_vline(xintercept = (mean(mtcars$mpg) + 2*sd(mtcars$mpg)), color = "red", linetype = "dotted")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 2016-11-26
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多