R ggplot2:没有绘制正确的数据和geom_path没有绘图

时间:2014-09-12 12:12:04

标签: r ggplot2

我试图绘制一个多元素变化图,因此具有离散的x轴和连续的y轴。我有两个问题:
- 虽然之前我已成功使用过类似的代码,但geom_path命令并没有绘制任何内容 - geom_point函数正在绘制错误的数据。第一点应为212,但正在绘制< 10

我已经尝试了两种不同的数据和代码布局,我在下面列出但每种都会生成相同的图像(附件)
非常感谢任何帮助 冬青

数据集样本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)

Plot resulting from both code sets above. Line is not plotted and points does not plot 'sample a' data.

1 个答案:

答案 0 :(得分: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("")

一般情况下,请避免在[内使用aesaes做了一些花哨的评价。如果您需要数据采用特定形式,则需要先在ggplot之外进行操作。这将更清洁,更不容易出错。在这种情况下,只需预先设置您想要的数据。

使用因子和级别排序控制离散变量顺序。

我不确定您要使用yminymax完成的尝试。我想也许你真的想用ylim()设置情节限制?如果你想展开情节,一个好方法就是添加一个&#34;虚拟&#34;数据框并使用geom_blank

您的数据框架运行良好的原因是因为行名称属性不是正确的长度。我的猜测是你试图手动编写structure()电话,而不是仅仅依靠dput