2017-08-07 44 views
-1

我試圖將3個矩陣合併到一個繪圖。使用ggplot2將3個矩陣轉換爲1個地圖

我試圖模擬標誌重捕場景。但是,不是有1個人口,而是有3個(每個矩陣都包含這3個人口)。

因爲我想一次從每個羣體來樣,x軸的範圍從0-300。從人口3

101-200從人口2

201-300:然而,1-100在x軸將對應於從人口採集的樣品。與圖片唯一的偏差是,我想要一條連續的線,從0-300。

我的代碼來創建這些矩陣,並提出每個矩陣的大小相同,但我不知道如何1)轉換和使用GGPLOT2 2繪製他們)把所有三個在一個圖

## Population size 
N <- 400 
N 

## Vector labeling each item in the population 
pop <- c(1:N) 
pop 

## Lower and upper bounds of sample size 
lower.bound <- round(x = .05 * N, digits = 0) 
lower.bound ## Smallest possible sample size 

upper.bound <- round(x = .15 * N, digits = 0) 
upper.bound ## Largest possible sample size 

## Length of sample size interval 
length.ss.interval <- length(c(lower.bound:upper.bound)) 
length.ss.interval ## total possible sample sizes, ranging form lower.bound to upper.bound 

## Determine a sample size randomly (not a global variable...simply for test purposes) 
## Between lower and upper bounds set previously 
## Give equal weight to each possible sample size in this interval 
sample(x = c(lower.bound:upper.bound), 
     size = 1, 
     prob = c(rep(1/length.ss.interval, length.ss.interval))) 

## Specify number of samples to take 
n.samples <- 100 

## Initiate empty matrix 
## 1st column is population (item 1 thorugh item 400) 
## 2nd through nth column are all rounds of sampling 
dat <- matrix(data = NA, 
       nrow = length(pop), 
       ncol = n.samples + 1) 

dat[,1] <- pop 


## Take samples of random sizes 
## Record results in columns 2 through n 
## 1 = sampled (marked) 
## 0 = not sampled (not marked) 
for(i in 2:ncol(dat)) { 
    a.sample <- sample(x = pop, 
        size = sample(x = c(lower.bound:upper.bound), 
            size = 1, 
            prob = c(rep(1/length.ss.interval, length.ss.interval))), 
        replace = FALSE) 
    dat[,i] <- dat[,1] %in% a.sample 
} 

## How large was each sample size? 
apply(X = dat, MARGIN = 2, FUN = sum) 
## 1st element is irrelevant 
## 2nd element through nth element: sample size for each of the 100 samples 


schnabel.comp <- data.frame(sample = 1:n.samples, 
          n.sampled = apply(X = dat, MARGIN = 2, FUN = sum)[2:length(apply(X = dat, MARGIN = 2, FUN = sum))] 
) 

## First column: which sample, 1-100 
## Second column: number selected in that sample 


## How many items were previously sampled? 
## For 1st sample, it's 0 
## For 2nd sample, code is different than for remaning samples 

n.prev.sampled <- c(0, rep(NA, n.samples-1)) 
n.prev.sampled 

n.prev.sampled[2] <- sum(ifelse(test = dat[,3] == 1 & dat[,2] == 1, 
           yes = 1, 
           no = 0)) 

n.prev.sampled 

for(i in 4:ncol(dat)) { 
    n.prev.sampled[i-1] <- sum(ifelse(test = dat[,i] == 1 & rowSums(dat[,2:(i-1)]) > 0, 
            yes = 1, 
            no = 0)) 
} 

schnabel.comp$n.prev.sampled <- n.prev.sampled 

## n.newly.sampled: in each sample, how many items were newly sampled? 
## i.e., never seen before? 
schnabel.comp$n.newly.sampled <- with(schnabel.comp, 
             n.sampled - n.prev.sampled) 

## cum.sampled: how many total items have you seen? 
schnabel.comp$cum.sampled <- c(0, cumsum(schnabel.comp$n.newly.sampled)[2:n.samples-1]) 

## numerator of schnabel formula 
schnabel.comp$numerator <- with(schnabel.comp, 
           n.sampled * cum.sampled) 

## denominator of schnable formula is n.prev.sampled 

## pop.estimate -- after each sample (starting with 2nd -- need at least two samples) 
schnabel.comp$pop.estimate <- NA 

for(i in 1:length(schnabel.comp$pop.estimate)) { 
    schnabel.comp$pop.estimate[i] <- sum(schnabel.comp$numerator[1:i])/sum(schnabel.comp$n.prev.sampled[1:i]) 
} 


## Plot population estimate after each sample 
if (!require("ggplot2")) {install.packages("ggplot2"); require("ggplot2")} 
if (!require("scales")) {install.packages("scales"); require("scales")} 


small.sample.dat <- schnabel.comp 

small.sample <- ggplot(data = small.sample.dat, 
         mapping = aes(x = sample, y = pop.estimate)) + 
    geom_point(size = 2) + 
    geom_line() + 
    geom_hline(yintercept = N, col = "red", lwd = 1) + 
    coord_cartesian(xlim = c(0:100), ylim = c(300:500)) + 
    scale_x_continuous(breaks = pretty_breaks(11)) + 
    scale_y_continuous(breaks = pretty_breaks(11)) + 
    labs(x = "\nSample", y = "Population estimate\n", 
     title = "Sample sizes are between 5% and 15%\nof the population") + 
    theme_bw(base_size = 12) + 
    theme(aspect.ratio = 1) 

small.sample 
+2

你能畫出一幅你想要圖形看起來的圖嗎?它不清楚你的矩陣如何與ggplot美學和geoms ... – Spacedman

+0

對不起!加入我創建了地塊的形象,我剛纔遇到問題結合他們 –

+0

我不想覆蓋他們 - 我想結合成估計 –

回答

0

看來你想做的事就是......

考慮三個數據幀是這樣的:

> d1 
    x   y 
1 1 0.899683096 
2 2 0.604513234 
3 3 0.005824789 
4 4 0.442692758 
5 5 0.103125175 
> d2 
    x   y 
1 1 0.35260029 
2 2 0.06248654 
3 3 0.79272047 
> d3 
    x   y 
1 1 0.4791399 
2 2 0.2583674 
3 3 0.1283629 
4 4 0.7133847 

建設d

> d = rbind(d1,d2,d3) 
> d$x = 1:nrow(d) 
> d 
    x   y 
1 1 0.899683096 
2 2 0.604513234 
3 3 0.005824789 
4 4 0.442692758 
5 5 0.103125175 
6 6 0.352600287 
7 7 0.062486543 
8 8 0.792720473 
9 9 0.479139947 
10 10 0.258367356 
11 11 0.128362933 
12 12 0.713384651 

然後繪X反對Ÿ正常。