2017-05-09 47 views
0

我有以下的數據幀:如何創建用於多個發行小面積擬合使用ggplot


library(tidyverse) 
set.seed(1) 
df <- data_frame(
    genes = paste("Gene_",letters[0:10],sep=""), 
    X = abs(rnorm(10, 0, 1)), 
    Y = abs(rnorm(10, 0, 2)), 
    Z = abs(rnorm(10, 0, 4))) 

df 
#> # A tibble: 10 × 4 
#>  genes   X   Y   Z 
#>  <chr>  <dbl>  <dbl>  <dbl> 
#> 1 Gene_a 0.6264538 3.02356234 3.6759095 
#> 2 Gene_b 0.1836433 0.77968647 3.1285452 
#> 3 Gene_c 0.8356286 1.24248116 0.2982599 
#> 4 Gene_d 1.5952808 4.42939977 7.9574068 
#> 5 Gene_e 0.3295078 2.24986184 2.4793030 
#> 6 Gene_f 0.8204684 0.08986722 0.2245150 
#> 7 Gene_g 0.4874291 0.03238053 0.6231820 
#> 8 Gene_h 0.7383247 1.88767242 5.8830095 
#> 9 Gene_i 0.5757814 1.64244239 1.9126002 
#> 10 Gene_j 0.3053884 1.18780264 1.6717662 

這一個估計參數用於上述每個非基因柱:


alldf <- structure(list(var = c("X", "Y", "Z", "X", "Y", "Z"), mod_est_mean = c(0.649790620181318, 
    1.65651567796795, 2.78544973796179, -0.594317687136244, -0.116217058012966, 
    0.518234267967891), mod_est_sd = c(0.37898907459421, 1.27340261798159, 2.38265470031565, 
    0.583177003946691, 1.49404482354149, 1.14803152575931), logLik = c(-4.48690631961252, 
    -16.6063107770219, -22.8715381956973, -2.85356316184894, -17.0420856382274, 
    -20.7522156015569), dist_name = c("normal", "normal", "normal", "lognormal", 
    "lognormal", "lognormal")), .Names = c("var", "mod_est_mean", "mod_est_sd", 
    "logLik", "dist_name"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
    "data.frame")) 
alldf 
#> var mod_est_mean mod_est_sd  logLik dist_name 
#> 1 X 0.6497906 0.3789891 -4.486906 normal 
#> 2 Y 1.6565157 1.2734026 -16.606311 normal 
#> 3 Z 2.7854497 2.3826547 -22.871538 normal 
#> 4 X -0.5943177 0.5831770 -2.853563 lognormal 
#> 5 Y -0.1162171 1.4940448 -17.042086 lognormal 
#> 6 Z 0.5182343 1.1480315 -20.752216 lognormal 

我想要做的是爲X,Y,Z創建方面圖。每個方面包含3行(實際數據,正常擬合,對數正態擬合)。

我能做的只是爲X

#Plot just one distribution (df$X) 
x <- df$X 
den <- density(x) 
df_x <- data.frame(x = den$x, y = den$y) 
df_x 
norm_param <- alldf %>% filter(var=="X", dist_name=="normal") 
lognorm_param <- alldf %>% filter(var=="X", dist_name=="lognormal") 
ggplot(df_x, aes(x = x,y = y)) + 
    geom_point(size = 3) + 
    geom_line(aes(x=df_x$x, y=dnorm(x=df_x$x, mean=norm_param$mod_est_mean, sd=norm_param$mod_est_sd)), color="red", size = 1) + 
    geom_line(aes(x=df_x$x, y=dnorm(x=log(df_x$x), mean=lognorm_param$mod_est_mean, sd=lognorm_param$mod_est_sd)), color="blue", size = 1) + 
    theme_classic() 
    # And how can I include legend for 3 lines? 

enter image description here

黑色(實際數據),紅色(正常FIT),藍(對數正態分佈FIT)

但我怎麼能做到這一點方面?

+1

爲了讓你在正確的軌道上,嘗試通過未經過濾的數據幀放入'ggplot',而不是先過濾。然後通過將'col = dist_name'添加到'aes()'中來獲得你的3行,並且通過在整體情節創建中加入'+ facet_grid(。〜var)'來實現你的方面。這對你有意義嗎? – rosscova

回答

2

這是一種方法。基本上你只是想盡可能的傳遞一個長數據幀到ggplot。根據您的數據製作長數據框。我會在這裏用lapply做你的計算,對每行alldf進行計算,得到你需要的3個獨立的數據集,用它們代表的內容標記它們,然後將這些數據集綁定到一個長的數據集中。

df <- lapply(X = seq_len(nrow(alldf)), 
       FUN = function(x) { 
        df <- data.frame(
         x = df_x$x, 
         y = df_x$y 
       ) 
        if(alldf$dist_name[x] == "normal") { 
         df$d_norm <- dnorm(x = df_x$x, mean = alldf$mod_est_mean[x], sd = alldf$mod_est_sd[x]) 
        } else if(alldf$dist_name[x] == "lognormal") { 
         df$d_norm <- dnorm(x = log(df_x$x), mean = alldf$mod_est_mean[x], sd = alldf$mod_est_sd[x]) 
        } 
        df$var <- alldf$var[x] 
        df$dist_name <- alldf$dist_name[x] 
        df 
       }) 
df <- do.call(rbind, df) 

head(df) 
      x   y  d_norm var dist_name 
1 -0.3642300 0.002889036 0.02936093 X normal 
2 -0.3593232 0.003142633 0.03039330 X normal 
3 -0.3544164 0.003412660 0.03145670 X normal 
4 -0.3495096 0.003697873 0.03255185 X normal 
5 -0.3446028 0.004015857 0.03367948 X normal 
6 -0.3396959 0.004349725 0.03484033 X normal 

現在你有一些整齊的數據,你可以把它傳遞給ggplot,告訴它通過把在爲aes參數根據dist_name,使顏色,並通過它告訴給facetvar volumn值(在這種情況下,作爲該「網格」的「列」)

ggplot(df) + 
    geom_point(aes(x = x, y = y), size = 3) + 
    geom_line(aes(x = x, y = d_norm, col = dist_name), size = 1) + 
    theme_classic() + 
    facet_grid(. ~ var) 

enter image description here

+0

我認爲你不得不對待對數正態分佈看看我的OP,特別是這行'geom_line(aes(x = df_x $ x,y = dnorm(x = log(df_x $ x),mean = lognorm_param $ mod_est_mean,sd = lognorm_param $ mod_est_sd)),color =「blue」,size = 1)' – neversaint

+0

啊我明白了,我錯過了。我已經在循環中添加了一個條件來解釋它,這是否看起來像你之後?這三個圖看起來使用您的代碼匹配輸出(分別將「X」更改爲「Y」和「Z」) – rosscova

+0

注意除了繪圖調用之外,我已經使用了base R。如果你願意,你當然可以用'tidyverse'工具來做到這一點。 – rosscova

相關問題