【问题标题】:ggplot() has error: missing value where TRUE/FALSE neededggplot() 有错误:需要 TRUE/FALSE 的地方缺少值
【发布时间】:2019-04-27 10:28:05
【问题描述】:

我正在尝试使用ggplot()。 Percentage 的数据类型是 Double。在数据集中,百分比和年份列中都没有 NA。

l1 <- ggplot(data, aes(Year, Percentage)) + 
  scale_x_discrete(name="Year From 2015 to 2018") + 
  scale_y_discrete(name="Employment Outcomes")

错误说:

Error in if (zero_range(from) || zero_range(to)) { : 
  missing value where TRUE/FALSE needed

【问题讨论】:

  • 为了让您的问题reproducible,您可以编辑您的问题以包含来自dput(data) 的输出吗?此外,您还没有包含任何 geom_* / stat_* 层。你打算画什么?

标签: r ggplot2


【解决方案1】:

在您的情况下,您需要指定要生成的可视化类型。例如,如果你想可视化时间序列图,脚本应该是这样的。

ggplot(data, aes(Year, Percentage)) + 
  geom_line() +
  scale_x_discrete(name="Year From 2015 to 2018") + 
  scale_y_discrete(name="Employment Outcomes")

基本上,您需要在ggplot() 之后指定geom_*()。 另外,我的另一个建议是,使用 labs() 而不是 scale_x_discrete() 在 X/Y 轴上添加名称。

ggplot(data, aes(Year, Percentage)) + 
  geom_line() +
  labs(x = "Year From 2015 to 2018",
       y = "Employment Outcomes")

这会生成您想要的类似情节。

【讨论】:

    猜你喜欢
    • 2019-02-25
    • 2013-02-24
    • 2018-08-09
    • 2012-10-11
    • 2020-04-22
    • 1970-01-01
    • 2023-01-17
    • 2011-11-13
    • 2020-07-29
    相关资源
    最近更新 更多