用ggplot镶嵌面_包裹覆盖R中的不同V线

用ggplot镶嵌面_包裹覆盖R中的不同V线,r,plot,ggplot2,facet-wrap,R,Plot,Ggplot2,Facet Wrap,我试图绘制一组密度图,显示四种细胞类型中两组基因表达水平分布的差异。除了密度图外,我还想将两组的中值表达水平叠加到每个图上。根据对几个类似问题的回答,我已经能够得到正确的图或正确的中间点,但不能同时得到两者。我没有主意了,希望有人能纠正我。谢谢 示例数据可在以下位置获得: 第一次尝试。生成正确的打印,但在所有四个上打印相同的中间点: dat = read.table("sample.data") g = ggplot(dat[which(dat$FPKM > 0),], aes(x =

我试图绘制一组密度图,显示四种细胞类型中两组基因表达水平分布的差异。除了密度图外,我还想将两组的中值表达水平叠加到每个图上。根据对几个类似问题的回答,我已经能够得到正确的图或正确的中间点,但不能同时得到两者。我没有主意了,希望有人能纠正我。谢谢

示例数据可在以下位置获得:

第一次尝试。生成正确的打印,但在所有四个上打印相同的中间点:

dat = read.table("sample.data")

g = ggplot(dat[which(dat$FPKM > 0),], aes(x = FPKM))
g = g + geom_density(aes(y = ..density.., group = class, color = class, fill = class), alpha=0.2)
g = g + geom_vline(data=dat, aes(xintercept = median(dat$FPKM[ which(dat$FPKM > 0 & dat$class == "Other") ]) ), colour="turquoise3", linetype="longdash")
g = g + geom_vline(data=dat, aes(xintercept = median(dat$FPKM[ which(dat$FPKM > 0 & dat$class == "a_MCKG") ]) ), colour="tomato1", linetype="longdash")
g = g + facet_wrap(~source, ncol=2, scales="free")
g = g + ggtitle("Distribution of FPKM, MCKG vs. Other")
g = g + xlab("FPKM > 0")
第二次尝试:更正绘图,但在所有绘图上放置所有中间点:

dat = read.table("sample.data")
vline.dat = data.frame(z=levels(dat$source), vl=tapply(dat$FPKM[which(dat$class != "a_MCKG" & dat$FPKM > 0)], dat$source[which(dat$class != "a_MCKG" & dat$FPKM > 0)], median), vm=tapply(dat$FPKM[which(dat$class == "a_MCKG" & dat$FPKM > 0)], dat$source[which(dat$class == "a_MCKG" & dat$FPKM > 0)], median))

g = ggplot(dat[which(dat$FPKM > 0),], aes(x = FPKM))
g = g + geom_density(aes(y = ..density.., group = class, color = class, fill = class), alpha=0.2)
g = g + facet_wrap(~source, ncol=2, scales="free")
g = g + geom_vline(data=vline.dat, aes(xintercept = vl), colour="turquoise3", linetype="longdash")
g = g + geom_vline(data=vline.dat, aes(xintercept = vm), colour="tomato1", linetype="longdash")
g = g + facet_wrap(~source, ncol=2, scales="free")
g = g + ggtitle("Distribution of FPKM, MCKG vs. Other")
g = g + xlab("FPKM > 0")
第三次尝试:绘图都是相同的,但有正确的中间点

dat = read.table("sample.data")
vline.dat = data.frame(z=levels(dat$source), vl=tapply(dat$FPKM[which(dat$class != "a_MCKG" & dat$FPKM > 0)], dat$source[which(dat$class != "a_MCKG" & dat$FPKM > 0)], median), vm=tapply(dat$FPKM[which(dat$class == "a_MCKG" & dat$FPKM > 0)], dat$source[which(dat$class == "a_MCKG" & dat$FPKM > 0)], median))

g = ggplot(dat[which(dat$FPKM > 0),], aes(x = FPKM))
g = g + geom_density(aes(y = ..density.., group = class, color = class, fill = class), alpha=0.2)
g = g + facet_wrap(~source, ncol=2, scales="free")
g = g + geom_vline(data=vline.dat, aes(xintercept = vl), colour="turquoise3", linetype="longdash")
g = g + geom_vline(data=vline.dat, aes(xintercept = vm), colour="tomato1", linetype="longdash")
g = g + facet_wrap(~z, ncol=2, scales="free")
g = g + ggtitle("Distribution of FPKM, MCKG vs. Other")
g = g + xlab("FPKM > 0")

传递预先汇总的数据是一种方法:

library(plyr)

names(dat) <- c("FPKM", "class", "source")
dat2 <- subset(dat, FPKM > 0)

ggplot(dat2, aes(x = FPKM)) + 
  geom_density(aes(y = ..density.., group = class, color = class, fill = class), alpha=0.2) +
  geom_vline(data = ddply(dat2, .(source, class), summarize, mmed = median(FPKM)),
             aes(xintercept = mmed, color = class)) +
  facet_wrap(~ source, ncol = 2, scales = "free") +
  ggtitle("Distribution of FPKM, MCKG vs. Other") +
  xlab("FPKM > 0")
库(plyr)

名称(dat)传递预先汇总的数据是一种方法:

library(plyr)

names(dat) <- c("FPKM", "class", "source")
dat2 <- subset(dat, FPKM > 0)

ggplot(dat2, aes(x = FPKM)) + 
  geom_density(aes(y = ..density.., group = class, color = class, fill = class), alpha=0.2) +
  geom_vline(data = ddply(dat2, .(source, class), summarize, mmed = median(FPKM)),
             aes(xintercept = mmed, color = class)) +
  facet_wrap(~ source, ncol = 2, scales = "free") +
  ggtitle("Distribution of FPKM, MCKG vs. Other") +
  xlab("FPKM > 0")
库(plyr)

名称(dat)示例数据没有标题-哪列是哪列?很抱歉。修复了git上的文件。您可以使用此选项直接在R:
colnames(dat)=c(“FPKM”、“class”、“source”)
示例数据没有标题-哪列是哪列?很抱歉。修复了git上的文件。您可以使用它直接在R:
colnames(dat)=c(“FPKM”、“class”、“source”)中修复它们。
谢谢!工作起来很有魅力!谢谢工作起来很有魅力!