我正在尝试从数据框中创建一个多面图,显示一个变量如何随其他变量而变化。每个变量都有一个与之相关的错误。这是我拥有的数据的子集; ID列,变量(SiO2到FeO)和与变量相关的错误(* _2stdev):
df<-structure(list(ID = structure(c(3L, 4L, 6L, 1L, 2L, 10L), .Label = c("P466-an1", "P466-an2", "P468-an1", "P468-an2", "P469-an1", "P470-an1", "P471-an1", "P472-an1", "P473-an1", "P474-an1", "P475-an1", "P475-an2", "P476-an1", "P476-an2", "P477-an1", "P478-an1", "P479-an1", "P480-an1"), class = "factor"),
SiO2 = c(54.5147, 56.2223, 52.8499, 52.0293, 53.4221, 52.9802),
TiO2 = c(0.5928, 0.5792, 0.5771, 1.1373, 1.0962, 1.1535),
Al2O3 = c(17.5404, 18.1921, 19.4737, 15.7752, 16.455, 16.4117),
FeO = c(6.2115, 5.8676, 5.4874, 4.5952, 4.4242, 4.109),
SiO2_2stdev = c(1.5232, 2.3578, 0.6374, 1.3331, 0.6535, 0.6977),
TiO2_2stdev = c(0.0638, 0.0637, 0.0357, 0.1024, 0.0422, 0.0282),
Al2O3_2stdev = c(0.4519, 0.4572, 0.2044, 0.6378, 0.6546, 0.0624),
FeO_2stdev = c(0.426, 0.3973, 0.1145, 0.1992, 0.1106, 0.0427)),
.Names = c("ID", "SiO2", "TiO2", "Al2O3", "FeO", "SiO2_2stdev", "TiO2_2stdev", "Al2O3_2stdev", "FeO_2stdev"),
row.names = c(NA, 6L), class = "data.frame")
使用以下代码:
library(reshape2)
library(ggplot2)
m.df<-melt(df, id=c('ID','FeO'))
p<-ggplot(subset(m.df, variable %in% c('SiO2','TiO2','Al2O3')),aes(x=value, y=FeO))+
geom_point()+
facet_wrap(~ variable, ncol=1, scales="free_x")+
theme_bw()
p
我得到这个情节:
我想将错误栏(垂直和水平)添加到此,但我不知道如何在分面图上执行此操作。
使用geom_errorbar
和geom_errorbarh
,我已经能够从未融合的数据框中为各个图绘制这些图。我想我可以使用循环制作所有图,但我不知道如何使用此方法添加错误栏。此外,我想立刻看到所有的情节。
感谢阅读本文,非常感谢任何帮助! -R
修改
根据 aosmith 的评论,我已将FeO_2stdev
添加到melt
中的ID变量中。我现在能够使用correc垂直错误栏生成一个图。所以现在我无法理解如何让geom_errorbarh
为每个情节绘制正确的错误标记。
以下是我正在使用的更新代码,以及生成的图表。
library(reshape2)
library(ggplot2)
m.df<-melt(df, id=c('ID','FeO', 'FeO_2stdev'))
m.df$y.min<-m.df$FeO-m.df$FeO_2stdev
m.df$y.max<-m.df$FeO+m.df$FeO_2stdev
p<-ggplot(subset(m.df, variable %in% c('SiO2','TiO2','Al2O3')), aes(x=value, y=FeO))+
geom_point()+
facet_wrap(~ variable, ncol=1, scales="free_x")+
theme_bw()+
geom_errorbar(aes(ymin=y.min, ymax=y.max))
p
答案 0 :(得分:1)
具有一列值和一列2个标准差的数据集将有助于水平误差线。这本质上是一个数据操作问题。有很多方法可以实现这样的目标。我将 tidyr 与 dplyr 一起使用。
例如,如果您在融化后立即以m.df
开头,则可以
separate
表示值,然后使用mutate
代表if_else
。spread
数据集返回宽格式,其中一列为值,另一列为2标准差。如果您已经熟悉它,也可以使用 reshape2 中的dcast
。库(dplyr) 库(tidyr)
m.df %>%
separate(variable, c("variable", "metric")) %>%
mutate(metric = if_else(is.na(metric), "value", metric)) %>%
spread(metric, value)
ID FeO FeO_2stdev variable 2stdev value
1 P466-an1 4.5952 0.1992 Al2O3 0.6378 15.7752
2 P466-an1 4.5952 0.1992 SiO2 1.3331 52.0293
3 P466-an1 4.5952 0.1992 TiO2 0.1024 1.1373
4 P466-an2 4.4242 0.1106 Al2O3 0.6546 16.4550
5 P466-an2 4.4242 0.1106 SiO2 0.6535 53.4221
...
以下是使用 tidyr 中的gather
而不是melt
获得相同结果的整个过程:
df2 = df %>%
gather(key, value, -ID, -contains("FeO")) %>%
separate(key, c("variable", "metric")) %>%
mutate(metric = if_else(is.na(metric), "value", metric)) %>%
spread(metric, value)
现在可以使用value
和2stdev
将水平误差线添加到地块中。请注意,列名2stdev
在语法上不正确,因此我在变量名称周围使用反引号。
ggplot(df2, aes(x=value, y=FeO))+
geom_point()+
facet_wrap(~ variable, ncol=1, scales="free_x")+
theme_bw() +
geom_errorbar(aes(ymin = FeO - FeO_2stdev, ymax = FeO + FeO_2stdev)) +
geom_errorbarh(aes(xmin = value - `2stdev`, xmax = value + `2stdev`))