【问题标题】:Error: Discrete value supplied to continuous scale problem错误:提供给连续规模问题的离散值
【发布时间】:2022-01-16 06:29:34
【问题描述】:

我有一个数据集,我试图从中创建一个图表,绘制五年内四个变量的发展。年份变量是字符,但其他变量是数字。 当我尝试绘制 ggplot 时,我收到错误消息:

错误:提供给连续刻度的离散值

创建ggplot的代码:

ggp <- ggplot(yearlywindhcgasbio, aes(year))+geom_line(aes(y = Wind, (size = 1.5)), group = 1)+geom_line(aes(y = Hard_coal), group = 2)+geom_line(aes(y = Gas), group = 3)+geom_line(aes(y = Bio), group = 4)

数据:

   year     Wind Hard_coal      Gas      Bio
1: 2015 236.2378  591.1061 596.0468 883.9906
2: 2016 325.8156  811.5624 454.8719 841.1440
3: 2018 615.1742  681.8199 570.9216 731.3470
4: 2019 647.8811  532.7532 512.6783 678.8823
5: 2020 821.2766  344.1962 472.8535 680.0227

我该如何解决这个问题?

【问题讨论】:

  • 你能提供一个可重现的例子吗?在不手动复制数据的情况下更容易提供帮助。请使用“代码”格式。谢谢。
  • 无法重现,对我来说运行良好,但数据不足。使用dput 进行数据共享。

标签: r ggplot2


【解决方案1】:

由于您的 year 列属于 character 类,ggplot 给出了错误

错误:提供给连续刻度的离散值

您需要添加以下行以使其工作。

scale_x_discrete()

您的代码将如下所示:

ggp <- ggplot(yearlywindhcgasbio, aes(year)) + 
  geom_line(aes(y = Wind, (size = 1.5)), group = 1) + 
  geom_line(aes(y = Hard_coal), group = 2) + 
  geom_line(aes(y = Gas), group = 3) + 
  geom_line(aes(y = Bio), group = 4) +
  scale_x_discrete()

【讨论】:

  • 万分感谢!
  • 请勾选答案旁边的勾选按钮来帮助我!
猜你喜欢
  • 2015-01-08
  • 2014-11-14
  • 2016-07-26
  • 2014-07-06
  • 2019-01-04
  • 1970-01-01
  • 2023-01-31
  • 1970-01-01
相关资源
最近更新 更多