Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

r - 非时间数据的R运行平均值(R running average for non-time data)

This is the plot I'm having now.

(这是我现在的情节。) 在此处输入图片说明

It's generated from this code:

(它是从以下代码生成的:)

ggplot(data1, aes(x=POS,y=DIFF,colour=GT)) + 
  geom_point() +
  facet_grid(~ CHROM,scales="free_x",space="free_x") + 
  theme(strip.text.x = element_text(size=40),
        strip.background = element_rect(color='lightblue',fill='lightblue'),
        legend.position="top",
        legend.title = element_text(size=40,colour="lightblue"),
        legend.text = element_text(size=40),
        legend.key.size = unit(2.5, "cm")) +
  guides(fill = guide_legend(title.position="top",
                             title = "Legend:GT='REF'+'ALT'"),
         shape = guide_legend(override.aes=list(size=10))) +
  scale_y_log10(breaks=trans_breaks("log10", function(x) 10^x, n=10)) + 
  scale_x_continuous(breaks = pretty_breaks(n=3)) +
  geom_line(stat = "hline",
            yintercept = "mean",
            size = 1)

The last line, geom_line creates the mean line for each panel.

(最后一行geom_line为每个面板创建平均线。)

But now I want to have the more specific running average inside each panel.

(但是现在我想在每个面板中都有更具体的运行平均值。)

ie If panel1('chr01') has x-axis range from 0 to 100,000,000, I would want to have the mean value for each 1,000,000 range.

(即,如果panel1('chr01') x轴范围是0到100,000,000,我希望每个1,000,000范围的平均值。)

mean1 = mean(x=0 to x=1,000,000)

mean2 = mean(x=1,000,001 to x=2,000,000)
  ask by Yilun Zhang translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way to provide a running mean is with geom_smooth() using the loess local regression method.

(提供运行平均值的一种方法是使用loess局部回归方法使用geom_smooth() 。)

In order to demonstrate my proposed solution, I created a fake genomic dataset using R functions.

(为了演示我提出的解决方案,我使用R函数创建了一个伪造的基因组数据集。)

You can adjust the span parameter of geom_smooth to make the running mean smoother (closer to 1.0) or rougher (closer to 1/number of data points).

(您可以调整geom_smoothspan参数,以使运行平均值更平滑(更接近1.0)或更粗糙(更接近1 /数据点数)。)

# Create example data.
set.seed(27182)

y1 = rnorm(10000) + 
     c(rep(0, 1000), dnorm(seq(-2, 5, length.out=8000)) * 3, rep(0, 1000))
y2 = c(rnorm(2000), rnorm(1000, mean=1.5), rnorm(1000, mean=-1, sd=2), 
       rnorm(2000, sd=2))
y3 = rnorm(4000)
pos = c(sort(runif(10000, min=0, max=1e8)),
        sort(runif(6000,  min=0, max=6e7)),
        sort(runif(4000,  min=0, max=4e7)))
chr = rep(c("chr01", "chr02", "chr03"), c(10000, 6000, 4000))

data1 = data.frame(CHROM=chr, POS=pos, DIFF=c(y1, y2, y3))

# Plot.
p = ggplot(data1, aes(x=POS, y=DIFF)) +
    geom_point(alpha=0.1, size=1.5) +
    geom_smooth(colour="darkgoldenrod1", size=1.5, method="loess", degree=0, 
        span=0.1, se=FALSE) +
    scale_x_continuous(breaks=seq(1e7, 3e8, 1e7), 
        labels=paste(seq(10, 300, 10)), expand=c(0, 0)) +
    xlab("Position, Megabases") +
    theme(axis.text.x=element_text(size=8)) +
    facet_grid(. ~ CHROM, scales="free", space="free")

ggsave(filename="plot_1.png", plot=p, width=10, height=5, dpi=150)

在此处输入图片说明


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...