【问题标题】:TimeSeries: Can't get the type = "o" to work, my data is only plotting horizontal lines for each data point when I need a connected line graphTimeSeries:无法让 type = "o" 工作,当我需要连接线图时,我的数据只为每个数据点绘制水平线
【发布时间】:2018-04-14 23:48:13
【问题描述】:

我正在尝试绘制时间序列图,但在将其显示为线图时遇到问题,同时在底部显示十年。

我的数据集在性能(整数)旁边有几十年(作为因素)

如果我写

plot(StockPerformance$Decade, StockPerformance$Performance)

我会得到一个有水平线的图表

PLOT PICTURE

添加,

type ="o"

像这样:

plot(StockPerformance$Decade, StockPerformance$Performance, type ="o")

不会改变它....

【问题讨论】:

  • 向我们展示可重现的东西。给我们你的真实数据的一个子集(使用dput)。你甚至没有告诉我们你使用了哪个函数,因为plot 是一个通用函数,而实际的函数取决于参数的类型......
  • 对不起,这是我发布的第一个问题。我使用了函数图。如何使用 dput?
  • 可能StockPerformance 是一个相当大的变量。将其缩小为可管理的内容,然后运行 ​​dput("StockPerformance")。 R 会打印一些其他人可以用来重新创建它的东西,然后我们可以回答你的问题。
  • dput如何使用?搜索“[r] 伟大的可重现示例”。这将是投票率最高的问题。这是minimal reproducible example的以R为中心的版本@

标签: r


【解决方案1】:

在 R 中,当您使用 read.table(或其变体)读取/创建数据框或使用 data.frame 创建数据框时,它会尝试找出您拥有的内容并适当地对待它。具体来说,带有字符向量的输入(如“1830s”)会被转换为因子。

因子是一种有效存储字符串的方法——这在 R 最初创建时比现在重要得多。对你来说重要的是角色没有任何顺序,除非你把它放在那里,所以 R 会自动用它们制作箱线图。这就是您看到线条的原因 - 它们是只有一个点的箱线图。

要解决这个问题,您需要将它们转换为数字以进行绘图。然后,您需要在之后“修复”轴。方法如下:

plot(Performance ~ as.numeric(Decade),
  data = StockPerformance,
  xlab = "Decade", # otherwise we have "as.numeric(Decade)
  xaxt = 'n', # removes default axis ticks and labels
  pch = 1 # default open circle.  Change the number to get other options.  16 and 20 are both closed circles (20 is small, 16 is big)
)
with(StockPerformance, # This just makes it so I don't have to type StockPerformance twice below.
  axis(1, at = 1:nlevels(Decade),
    value = levels(Decade)
))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2020-04-02
    相关资源
    最近更新 更多