【问题标题】:R ggplot2: Not plotting the right data and geom_path not plottingR ggplot2:未绘制正确的数据和 geom_path 未绘制
【发布时间】:2014-09-12 23:12:16
【问题描述】:

我正在尝试绘制多元素变化图,因此具有离散的 x 轴和连续的 y 轴。我有两个问题:
- 虽然我之前已经成功使用过类似的代码,但 geom_path 命令没有绘制任何东西
- geom_point 函数绘制了错误的数据。第一个点应该是 212,但正在绘制
我尝试了两种不同的数据和代码布局,我在下面包含了这些布局,但每种布局都产生了相同的图像(附加)。

任何帮助将不胜感激。
冬青

数据集样本1:

Chond_normalised <- structure(list(Element = c("Th", "Nb", "La", "Ce", "Pr", "Nd", "Sm", "Zr", "Eu", "Ti", "Gd", "Tb", "Dy", "Th", "Nb", "La", "Ce", "Pr", "Nd", "Sm", "Zr", "Eu", "Ti", "Gd", "Tb", "Dy", "Th", "Nb", "La", "Ce", "Pr", "Nd", "Sm", "Zr", "Eu", "Ti", "Gd", "Tb", "Dy"), ppm = c(212, 65, 73, 49, 38, 26, 12, 25, 6, 6, 7, 6, 5, 8, 10, 122, 95, 73, 55, 26, 4, 17, 1, 14, 9, 7, 41, 46, 74, 57, 49, 43, 28, 19, 20, 6, 18, 13, 9), Sample = c("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c")), .Names = c("Element", "ppm", "Sample"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L), class = "data.frame")
Chond_normalised

代码 1:

library(ggplot2)
Chond_normalised <- read.csv('filename.csv', header=TRUE, sep=",")
attach(Chond_normalised)
ggplot(data=Chond_normalised, aes(ymin=0.1, ymax=1000, x=Element, y=ppm)) +
geom_path(data=Chond_normalised, aes(y=ppm[Sample=="a"], x=Element[Sample=="a"]), colour="black", size=1.0) +
geom_point(data=Chond_normalised, aes(y=ppm[Sample=="a"], x=Element[Sample=="a"]), colour="red", size=1.0) +
scale_y_log10("Sample / Chondrite", breaks=c(0.1, 1, 10, 100, 1000)) +
scale_x_discrete("", labels=Element)

数据集样本2:

Chond_normal <- structure(list(Element = c("Th", "Nb", "La", "Ce", "Pr", "Nd", "Sm", "Zr", "Eu", "Ti", "Gd", "Tb", "Dy"), a = c(212, 65, 73, 49, 38, 26, 12, 25, 6, 6, 7, 6, 5), b= c(8, 10, 122, 95, 73, 55, 26, 4, 17, 1, 14, 9, 7)), .Names = c("Element", "a", "b"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L), class = "data.frame")
Chond_normal

代码 2:

library(ggplot2)
Chond_normal <- read.csv('file.csv', header=TRUE, sep=",")
attach(Chond_normal)
ggplot(data=Chond_normal, aes(ymin=0.1, ymax=1000, x=Element, y=Chond_normal[,2:26])) +
geom_path(data=Chond_normal, aes(y=a, x=Element), colour="black", size=1.0) +
geom_point(data=Chond_normal, aes(y=a, x=Element), colour="red", size=1.0) +
scale_y_log10("Sample / Chondrite", breaks=c(0.1, 1, 10, 100, 1000)) +
scale_x_discrete("", labels=Element)

【问题讨论】:

  • 您的两个数据集都不可重现。乍一看,您需要在绘图前reshape2::melt您的 data.frame。并且geom_的每一个函数如果不改就不需要设置数据了。
  • 抱歉,我已经编辑了数据集脚本,但无法让 R 接受元素列表作为文本而不是要搜索的对象。本质上,第一个数据集是 3 列:元素、ppm、样本 id。第二个数据集有很多列:元素、样本 a、b、c 等的 ppm。我在 geom_ 中设置数据的原因是因为我想为每个样本添加许多线和点,所以 y= 会改变每一行。
  • @HollyElliott 您是否在他的 data.frames 上使用dput() 以使其可重现?因为那些不能复制/粘贴到 R 中。我试图重新格式化它们以使它们更好,但如果我搞砸了,请告诉我。此外,使用 attach() 也不是一个好主意。这变得非常混乱,所以我已经将其注释掉并替换了我认为使用它的唯一代码。另外,所有这些主题内容是重现问题所必需的吗?我似乎只是在问题中添加了噪音。尝试提供最少代码以使问题可重现。
  • 实际上继续并回滚了我的更改。 data.frames 仍然存在行数和行名不匹配的问题。见how to make an R reproducible example
  • 我编辑了代码以尝试使其最小化,并且数据集 2 现在完全可重现,数据集 1 我仍然遇到问题,但代码仍然呈现数据外观。谢谢你!

标签: r ggplot2


【解决方案1】:

我不确定您为什么要在 ggplot 代码中执行某些操作,但请尝试类似这样的操作:

Chond_normalised <- structure(list(Element = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L), .Label = c("Th", "Nb", "La", "Ce", "Pr", "Nd", 
"Sm", "Zr", "Eu", "Ti", "Gd", "Tb", "Dy"), class = "factor"), 
    ppm = c(212, 65, 73, 49, 38, 26, 12, 25, 6, 6, 7, 6, 5, 8, 
    10, 122, 95, 73, 55, 26, 4, 17, 1, 14, 9, 7, 41, 46, 74, 
    57, 49, 43, 28, 19, 20, 6, 18, 13, 9), Sample = c("a", "a", 
    "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "b", 
    "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
    "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", 
    "c")), .Names = c("Element", "ppm", "Sample"), row.names = c(NA, 
-39L), class = "data.frame")

a <- Chond_normalised$Element[Chond_normalised$Sample == 'a']
Chond_normalised$Element <- factor(Chond_normalised$Element,levels = a)

ggplot(data=Chond_normalised[Chond_normalised$Sample == 'a',]) +
    geom_path(aes(y=ppm, x=Element,group = 1), colour="black", size=1.0) +
    geom_point(aes(y=ppm, x=Element), colour="red", size=1.0) +
    scale_y_log10("Sample / Chondrite", breaks=c(0.1, 1, 10, 100, 1000)) +
    scale_x_discrete("")

一般来说,避免在aes 中使用[aes 做了一些花哨的评估。如果您需要数据以特定形式存在,则需要先在 ggplot 之外进行操作。这样会更干净,更不容易出错。在这种情况下,只需预先对您想要的数据进行子集化。

用因子和水平顺序控制离散变量顺序。

我不确定你想用 yminymax 来完成什么。我想也许你真的想使用ylim() 来设置情节限制?如果您想扩展绘图,一个好方法是添加一个“虚拟”数据框并使用geom_blank

您的数据框运行不佳的原因是行名称属性的长度不正确。我的猜测是您试图手动编写 structure() 调用,而不是简单地依赖 dput

【讨论】:

  • 效果很好,非常感谢!!!!我花了很多时间来搞清楚代码的哪一部分导致了问题。我从大约一年前发现的几个不同示例中将它放在一起,它适用于其他数据框架,但不喜欢这个。再次感谢乔兰!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 2016-09-26
  • 1970-01-01
  • 2011-09-18
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
相关资源
最近更新 更多