簡體   English   中英

如何將多個密度曲線疊加到R中的一個圖中

[英]How to Superimpose Multiple Density Curves Into One Plot in R

我有一個看起來像這樣的數據。

我打算在一個圖中創建多個密度曲線,其中每個曲線對應於唯一ID。

我嘗試使用“sm”包,使用此代碼,但沒有成功。

library(sm)
dat <- read.table("mydat.txt");
plotfn <- ("~/Desktop/flowgram_superimposed.pdf");
pdf(plotfn);

sm.density.compare(dat$V1,dat$V2, xlab = "Flow Signal")
colfill <- c(2:10);
legend(locator(1), levels(dat$V2), fill=colfill)

dev.off();

請告知正確的方法是什么,或者是否有其他方法可以做到這一點?

我想在最后得到這種情節。 圖http://img524.imageshack.us/img524/2736/testl.png

嘗試使用ggplot2:

dnow <- read.table("http://dpaste.com/88561/plain/")
library(ggplot2)
qplot(V1, colour=factor(V2), data=dnow, geom="density")

您也可以使用晶格包解決這個問題。

require(lattice)
dnow <- read.table('http://dpaste.com/88561/plain/')
densityplot(~V1, groups=V2, data=dnow)

以意大利面條代碼方式使用基本圖形:

plot.multi.dens <- function(s)
{
junk.x = NULL
junk.y = NULL
for(i in 1:length(s))
{
junk.x = c(junk.x, density(s[[i]])$x)
junk.y = c(junk.y, density(s[[i]])$y)
}
xr <- range(junk.x)
yr <- range(junk.y)
plot(density(s[[1]]), xlim = xr, ylim = yr, main = "")
for(i in 1:length(s))
{
lines(density(s[[i]]), xlim = xr, ylim = yr, col = i)
}
}
dnow <- read.table("http://dpaste.com/88561/plain/")
library(sqldf)
x <- unlist(sqldf("select V1 from dnow where V2==0"))
y <- unlist(sqldf("select V1 from dnow where V2==1"))
z <- unlist(sqldf("select V1 from dnow where V2==2"))
plot.multi.dens(list(x,y,z))
library(Hmisc)
le <- largest.empty(x,y,.1,.1)
legend(le,legend=c("x","y","z"), col=(1:3), lwd=2, lty = 1)

我發現自己在查看微陣列數據時需要做很多事情,所以我把它作為我保留在github上的實用程序代碼庫的一部分: ARE.utils ,特別是plot.densities函數。

它使用基本圖形,因此您可以從該功能中獲取靈感來創建自己的圖形,或者只是將其全部銷售(但它依賴於該庫中的其他一些功能):

  1. create.densities ,它將數據的列表/矩陣/等轉換為密度列表;
  2. match.dim函數(將維度“名稱”轉換為數字軸)。

(你可以選擇安裝整個軟件包,但我不承諾我的功能不會以某種向后不兼容的方式改變)。

編寫自己的這樣的功能並不難,但只要確保你有功能在軸和東西上選擇正確的范圍。 無論如何,你會使用這樣的代碼:

library(ARE.utils)
# Create a matrix dataset with separate observations in columns
dat <- matrix(c(rnorm(100), rnorm(100, mean=3), 
                rnorm(100, mean=3, sd=2)),
              ncol=3)
# Plot them
plot.densities(dat, along='cols')

這將在同一軸上創建三種不同的密度圖,並具有自己的顏色。

暫無
暫無

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

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