【问题标题】:panel.abline of mean and panel.text of sample size on lattice histograms created in a loop循环中创建的晶格直方图上的均值 panel.abline 和样本大小的 panel.text
【发布时间】:2013-05-22 20:20:38
【问题描述】:

我正在使用for 循环创建一系列lattice 直方图,比较来自样本数据的长度-频率分布,并希望每个直方图显示一个垂直的abline,显示每个分布的平均值以及@ 987654324@表示样本号。

我有以下数据:

> head(hist.data,20)
               Scientific_name Count Length..cm. Method
3  Pristipomoides filamentosus     1          60 BotCam
5           Etelis carbunculus     1          43 BotCam
6             Etelis coruscans     1          40 BotCam
12 Pristipomoides filamentosus     1          55 BotCam
16           Aphareus rutilans     1          67 BotCam
17           Aphareus rutilans     1          77 BotCam
20          Etelis carbunculus     1          46 BotCam
21    Pristipomoides sieboldii     1          35 BotCam
23    Pristipomoides sieboldii     1          33 Fishing
25          Etelis carbunculus     1          53 Fishing
26 Pristipomoides filamentosus     1          45 Fishing
27 Pristipomoides filamentosus     1          43 Fishing
28 Pristipomoides filamentosus     1          58 Fishing
29 Pristipomoides filamentosus     1          55 Fishing
30    Pristipomoides sieboldii     1          29 Fishing

我的代码如下:

#create a list of species
sp <- c("Etelis coruscans","Etelis carbunculus","Pristipomoides sieboldii","Pristipomoides filamentosus","Pristipomoides zonatus","Epinephelus quernus","Aphareus rutilans")

#Calculate sample# and mean length by species by method
n <- with(hist.data, tapply(Scientific_name, Method, function(x) count(x)))
mean.length <- aggregate(Length..cm. ~ Scientific_name + Method, data = hist.data, FUN= "mean")

#plot hisotgrams for each spp in 1cm bins
for (i in sp){
    BIN_WIDTH <- 1 #desired bin width
    print(histogram(~ Length..cm. | Method, #create and print histogram
    data = hist.data[hist.data$Scientific_name == i,], 
    nint = (max(hist.data$Length..cm.) - min(hist.data$Length..cm.)+1)/BIN_WIDTH,
    layout = c(1,2),
    type = "density",
    main = substitute(expr = expression(paste("Length-Frequency of ", italic(i), " by Gear")), env = list(i=i)),
    xlab = "Length (cm)",
    panel = function(x, ...){
        #panel.abline(v = 60, col = "red", lty = 2)
        #panel.text(lab = paste("Sample #: ",n$BotCam[1,2]), 90, 100)
        panel.histogram(x,...)
        panel.mathdensity(dmath = dnorm, col = "black",
                      args = list(mean = mean(x), sd= sd(x)), ...)     
    }
    ))


    #save histogram as PDF file
    quartz.save(paste("Length-Frequency of", i, "by method.pdf", sep = " "), type = "pdf")
    dev.off() #close the graphics diver
}

我可以产生以下数组:

n <- with(hist.data, tapply(Scientific_name, Method, function(x) count(x)))
n
$BotCam
                            x freq
1           Aphareus rutilans   16
2          Etelis carbunculus   35
3            Etelis coruscans   20
4 Pristipomoides filamentosus  179
5    Pristipomoides sieboldii  125
6      Pristipomoides zonatus    2

$Fishing
                            x freq
1         Epinephelus quernus    2
2          Etelis carbunculus   68
3            Etelis coruscans   30
4 Pristipomoides filamentosus   24
5    Pristipomoides sieboldii   80
6      Pristipomoides zonatus    5

mean.length <- aggregate(Length..cm. ~ Scientific_name + Method, data = hist.data, FUN= "mean")
> mean.length
               Scientific_name  Method Length..cm.
1            Aphareus rutilans  BotCam    58.81250
2           Etelis carbunculus  BotCam    43.65714
3             Etelis coruscans  BotCam    46.55000
4  Pristipomoides filamentosus  BotCam    53.22346
5     Pristipomoides sieboldii  BotCam    35.52000
6       Pristipomoides zonatus  BotCam    35.00000
7          Epinephelus quernus Fishing    74.00000
8           Etelis carbunculus Fishing    42.98529
9             Etelis coruscans Fishing    49.96667
10 Pristipomoides filamentosus Fishing    59.58333
11    Pristipomoides sieboldii Fishing    37.25000
12      Pristipomoides zonatus Fishing    30.80000

我想更换:

#panel.abline(v = 60, col = "red", lty = 2)
#panel.text(lab = paste("Sample #: ",n$BotCam[1,2]), 90, 100)

使用根据物种分布的平均值生成abline 的代码和使用从mean.length 生成abline 的代码和从n 生成“样本#:”的代码text。上图和下图以及loop 的每次迭代的值将不同。有没有办法做到这一点?我不喜欢lattice,这只是我有一些经验。如果像 ggplot 这样的东西更适合这个,请告诉我。

【问题讨论】:

    标签: r histogram lattice


    【解决方案1】:

    在四处寻找之后,我想出了以下似乎可以解决问题的代码。现在我只需要找出定位panel.textoutput 的最佳方法,因为我的轴会随着循环的每次迭代而变化。

    #plot hisotgrams for each spp in 1cm bins
    for (i in sp){
        BIN_WIDTH <- 1 #desired bin width
        print(histogram(~ Length..cm. | Method, #create and print histogram
        data = hist.data[hist.data$Scientific_name == i,], 
        nint = (max(hist.data$Length..cm.) - min(hist.data$Length..cm.)+1)/BIN_WIDTH,
        layout = c(1,2),
        type = "density",
        main = substitute(expr = expression(paste("Length-Frequency of ", italic(i), " by Gear")), env = list(i=i)),
        xlab = "Length (cm)",
        panel = function(x, ...){
            mean.values <- mean(x)
            panel.abline(v=mean.values, col.line="red", lty = 2)
            sample.n <- length(x)
            panel.text(lab = paste("Sample size = ", sample.n), mean.values*1.2, .26)
            panel.text(lab = paste("Mean = ", round(mean.values, 1)), mean.values*1.2, .23)
            panel.histogram(x,...)
            panel.mathdensity(dmath = dnorm, col = "black",
                          args = list(mean = mean(x), sd= sd(x)), ...)     
        }
        ))
    
        #save histogram to PDF file
        quartz.save(paste("Length-Frequency of", i, "by method.png", sep = " "), type = "png")
        dev.off() #close graphics diver
    }
    

    【讨论】:

    • 谢谢。我正在考虑一个类似的问题。但是在我使用了像你这样的代码之后,我的图表显示“使用数据包 1 时出错,找不到对象‘x’。”你知道为什么吗?
    【解决方案2】:

    您可以上传您的代码副本吗?听起来您没有正确定义 (x)。

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多