【发布时间】: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 我仍然遇到问题,但代码仍然呈现数据外观。谢谢你!