簡體   English   中英

改進 ggplot2 中概率密度函數的繪圖

[英]improving plotting of probability density functions in ggplot2

我正在使用 ggplot 繪制多個已知的密度函數,例如伽馬密度 function:

library(tidyverse)
apar<-c(1,2,7.5,9)
bpar<-c(2,2,1.3,0.5)
gmaxlim<-c(0, 25)
pgma1<-ggplot(data = data.frame(x = gmaxlim), aes(gmaxlim)) +
  stat_function(fun = dgamma, n = 101, args = list(shape = apar[1], scale = bpar[1]),aes(color="black")) +
  stat_function(fun = dgamma, n = 101, args = list(shape = apar[2], scale = bpar[2]),aes(color="red")) +
  stat_function(fun = dgamma, n = 101, args = list(shape = apar[3], scale = bpar[3]),aes(color="blue")) +
  stat_function(fun = dgamma, n = 101, args = list(shape = apar[4], scale = bpar[4]),aes(color="green")) +
  ylab(expression(paste("f(x|",alpha,",",beta,")"))) +xlab("x") + scale_x_continuous(breaks=seq(gmaxlim[1],gmaxlim[2], by =5)) + 
  scale_color_identity(name = "",
                       breaks = c("black", "red", "blue","green"),
                       labels = c(substitute(paste(alpha,"= ", v," ,",beta,"= ",s),list(v=apar[1],s=bpar[1])),
                                  substitute(paste(alpha,"= ", v," ,",beta,"= ",s),list(v=apar[2],s=bpar[2])), 
                                  substitute(paste(alpha,"= ", v," ,",beta,"= ",s),list(v=apar[3],s=bpar[3])),
                                  substitute(paste(alpha,"= ", v," ,",beta,"= ",s),list(v=apar[4],s=bpar[4]))),
                       guide = "legend")+
  theme_bw()
pgma1

代表 package (v0.3.0) 於 2020 年 7 月 31 日創建

然而,這段代碼遠非高效,而且違背了 ggplot 的理念(也許是因為我們沒有繪制任何“真實”數據集?)。 有沒有辦法更有效地編寫這個並且可以擴展到不同數量的參數對? 我只想有一行stat_function並在可能的情況下簡化scale_color_identity 在顏色標簽中保留數學表達式是強制性的

也許使用一些 lapply?

library(tidyverse)
apar <- c(1,2,7.5,9)
bpar <- c(2,2,1.3,0.5)
gmaxlim <- c(0, 25)
mycols <- c("black", "red", "blue", "green")

ggplot(data = data.frame(x = gmaxlim), aes(gmaxlim)) +
lapply(seq_along(apar), function(i){
    stat_function(fun = dgamma, n = 101, 
    args = list(shape = apar[i], scale = bpar[i]), aes( color=mycols[i]))
}) +
    scale_color_identity(name="", breaks = mycols,
    labels = lapply(seq_along(apar), function(i) 
        substitute(paste(alpha,"= ", v," ,",beta,"= ",s),
            list(v=apar[i], s=bpar[i]))), guide = "legend") +
    theme_bw()

代表 package (v0.3.0) 於 2020 年 7 月 31 日創建

我有點困惑為什么這么多人試圖用ggplot中的stat函數做這么多,而不是將他們真正想要的數據傳遞給 plot。 使用stat_function可以很好地直接繪制奇數線,但試圖強迫它做復雜的事情,比如通過引用外部向量來繪制分布族,這似乎很難做到。

更容易推理並且需要更少的代碼,只需計算出您想要 plot 和 plot 的內容:

apar <- c(1, 2, 7.5, 9)
bpar <- c(2, 2, 1.3, 0.5)
x    <- seq(0, 25, 0.25)
y    <- as.vector(sapply(1:4, function(i) dgamma(x, apar[i], scale = bpar[i])))
df   <- data.frame(x = rep(x, 4), y, group = rep(letters[1:4], each = length(x)))
labs <- sapply(1:4, function(i) {
               substitute(paste(alpha,"= ", v," ,",beta,"= ",s), 
               list(v = apar[i], s = bpar[i]))})

ggplot(data = df, aes(x, y)) + geom_line(aes(color = group)) +
  ylab(expression(paste("f(x|", alpha, ",", beta,")"))) +
  scale_color_manual(values = c(1, 2, 4, 3), labels = labs) +
  theme_bw()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM