【问题标题】:Y Axis wrong indexY轴错误索引
【发布时间】:2015-12-24 19:27:38
【问题描述】:

我正在读一本书,我发现了这段代码。我试过了,但我对我得到的图表有点困惑。

这是数据样本。

 consumption[sample(1:nrow(consumption), 5, replace=F),]
                    Food  Units Year    Amount
8  Fruits and Vegetables Pounds 1980 603.57948
31    Caloric sweeteners Pounds 1995 144.08113
16 Fruits and Vegetables Pounds 1985 630.24491
28                  Eggs Number 1995 232.28203
19    Fish and Shellfist Pounds 1990  14.94411

我得到了这张图。其中 Y 索引是从 1 到 20 的数字,不是正确的“数量”。

我该怎么做才能正确显示 Y 轴上的金额索引?

【问题讨论】:

  • 我需要我的眼镜 :) 你能否在你的帖子中复制代码而不是使用图像。您还可以使用 dput() 将数据集包含在您的帖子中。

标签: r graph dataframe lattice


【解决方案1】:

您展示的图就像书中的图,R in a Nutshell,它为您提供了代码。实际上,这本书提供了同一情节的两个不同版本的代码。我建议两个都试试。

library(nutshell)
data(consumption)

library(lattice)
dotplot(Amount ~ Year | Food, consumption)
dotplot(Amount ~ Year | Food, consumption, 
  aspect="xy", scales=list(relation="sliced", cex=.4))

【讨论】:

  • 是的,没什么。唯一的问题是 Y 轴显示的数字是从 1 到 49,而不是正确的数量,如 645 等。
  • dotplot 用于显示“类别”。如果您想以散点图的形式显示值,请尝试使用xyplot,例如xyplot(Amount ~ Year | Food, consumption)
猜你喜欢
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 2018-06-17
  • 1970-01-01
相关资源
最近更新 更多