【问题标题】:Plotting aggregate data in R在 R 中绘制聚合数据
【发布时间】:2017-03-30 21:05:05
【问题描述】:

我有一组数据,其中包含 358 个岩石样本及其密度、p 波速度和岩石类型(从 1 到 8 的类别)。

我使用聚合函数来找到每种岩石类型的平均密度和每种岩石类型的平均 p 波速度。

dens = aggregate(SONIC_TXT$Density_g.cm3, by=list(SONIC_TXT$Rock_type), mean)
vel = aggregate(SONIC_TXT$P.WaveVelocity_km.sec, by=list(SONIC_TXT$Rock_type), mean)

我现在想将这些结果绘制在每种岩石类型的平均密度与每种岩石类型的平均 p 波速度的散点图上。

我的尝试返回错误,说我需要“有限限制”。所以我在绘图函数中添加了限制,并得到了一个带有这些限制且没有点的图。我尝试了 ggplot 并没有出错,但也只是一个没有轴/边界/点/任何东西的“情节”。

谁能帮帮我?


编辑:我的尝试包括:

  1. plot(dens$vel)

    导致:

        Error in plot.window(...) : need finite 'xlim' values
    In addition: Warning messages:
    1: In min(x) : no non-missing arguments to min; returning Inf
    2: In max(x) : no non-missing arguments to max; returning -Inf
    3: In min(x) : no non-missing arguments to min; returning Inf
    4: In max(x) : no non-missing arguments to max; returning -Inf
    
  2. plot(dens$vel, xlim = c(0, 50), ylim = c(0,50),ylab ='ylab', xlab='xlab')

    导致:

  3. matplot(dens, cbind(dens$x,vel$x),type="p",col=c("red","green"),pch=16)

    结果:这个情节更符合我的目标。除了速度值现在不符合岩石类型。


编辑 2:

> dens2
  Rock_type Density_g.cm3
1         1      2.633226
2         2      2.677167
3         3      2.774167
4         4      2.919500
5         5      2.823643
6         6      2.794964
7         7      3.006226
8         8      3.240798

> vel2
  Rock_type P.WaveVelocity_km.sec
1         1              5.640581
2         2              5.803310
3         3              5.691533
4         4              6.426667
5         5              5.828643
6         6              6.217643
7         7              6.715594
8         8              7.556798

【问题讨论】:

  • 我们能看到那些试图帮助解决问题的阴谋吗?此外,您输入的几行内容也会有所帮助。
  • 除了我的帖子中的输入之外,我唯一真正的输入是上传和附加数据(SONIC_TXT,这是一个包含数据的文本文件)。并且没有要显示的情节。它们要么是错误,要么只是一个没有任何内容的情节。 (即它是一个盒子,在 x 和 y 轴上有正确的限制,没有点)。
  • 通过尝试,我的意思是你的情节代码,当然你可以用几行聚合数据框编辑你的帖子 -densvel .只需复制和粘贴代码格式{}
  • @Parfait 我已经编辑了原始帖子以包含我的尝试。

标签: r plot aggregate aggregate-functions


【解决方案1】:

在您的数据中,dens2 中没有 veldens2 是数据框,而不是向量。试试这个:

d <- data.frame(dens=dens2$Density_g.cm3, vel2=vel2$P.WaveVelocity_km.sec, 
                type=dens2$Rock_type)
windows()
  with(d, plot(dens, vel2, pch=as.character(type)))


(查看原始答案的修订历史,这是推测,现在已经过时了,因为有一个可重复的例子。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-23
    • 2020-03-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    相关资源
    最近更新 更多