2016-08-01 24 views
0

我試圖從數據框中創建小平面圖,顯示一個變量作爲其他變量的函數如何更改。每個變量都有一個與之相關的錯誤。這是我擁有的數據的一個子集。一個ID列,變量(二氧化硅爲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 

我得到這樣的情節:

plot with no errorbars

我想添加這個錯誤條(垂直和水平),但我不知道如何在一個分面圖上做到這一點。

使用geom_errorbargeom_errorbarh,我已經能夠繪製這些從未熔化的數據幀的個別情節。我想我可以用循環繪製所有圖,但我不知道如何使用此方法添加錯誤條。此外,我想一次看到所有的情節。

感謝您閱讀本文,非常感謝您的幫助! -R

編輯AOSMITH的評論,我在melt添加FeO_2stdev的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 

plot with vertical errorbars

+0

你是不是想添加基於'FeO_2stdev'錯誤吧?如果是這樣,請將此變量保存爲'melt'中的一個id變量,以便您可以在'geom_errorbar'中使用它。如果不是,你能澄清一下你用什麼來計算誤差線嗎? – aosmith

+0

啊,是的!我現在已經完成了這個工作,並且可以繪製FeO錯誤條。現在,我的問題是如何在每個圖上繪製合適的水平誤差線。我已經更新了這個問題來反映這一點。 – ramesesjd

回答

1

經與值一列,2個標準差一列的數據集將幫助與水平誤差棒。這本質上是一個數據操縱問題。有很多方法可以實現這樣的事情。我正在使用tidyrdplyr

例如,如果你融化後立即用m.df開始,你可以

  1. 創建一個新的變量來表示該行是否代表2度標準差,或使用separate,然後用if_elsemutate值。
  2. spread將數據集恢復爲寬格式,其中一列爲值,另一列爲2個標準差。如果您已經熟悉它,也可以使用dcastreshape2

庫(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 
... 

下面是使用gathertidyr的全過程,而不是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) 

現在水平可以使用value2stdev將誤差線添加到您的圖中。請注意,列名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`)) 

enter image description here

+0

這很棒!謝謝。有一個問題:爲什麼在'2stdev'周圍需要反引號,而在'geom_errorbarh'中不需要'value'? – ramesesjd

+0

在R中,以數字開頭的名稱在語法上不是有效的,所以'2stdev'必須包裝反標記,但'value'不包含。 'make.names'的[幫助頁面](https://stat.ethz.ch/R-manual/R-devel/library/base/html/make.names.html)包含一些關於語法有效名稱的有用信息。 – aosmith

相關問題