我有以下的數據幀:如何創建用於多個發行小面積擬合使用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?
黑色(實際數據),紅色(正常FIT),藍(對數正態分佈FIT)
但我怎麼能做到這一點方面?
爲了讓你在正確的軌道上,嘗試通過未經過濾的數據幀放入'ggplot',而不是先過濾。然後通過將'col = dist_name'添加到'aes()'中來獲得你的3行,並且通過在整體情節創建中加入'+ facet_grid(。〜var)'來實現你的方面。這對你有意義嗎? – rosscova