如何使用 R 中 gplot() 包中的 plotmean() 函数使用 paste0() 操作 n.label 值以获得观察次数

How to manipulate the n.label values with paste0() for the number of observations using the plotmean() function in the gplot() package in R

问题:

我有一个名为 FID 的数据框,我想标记每个均值的观察次数以及上下置信区间。因为数据框显示了三年(下图)中每月的观察次数,所以 n.labels 被标记为 n=3(见下图 1)。

我创建了两个名为 observations 和 month_level 的向量,我希望使用函数 paste0() 将真实的 n.label 值插入到情节(见R代码)。当我尝试将这些 n.label 值粘贴到绘图上时,连接平均值的绘图线往往会消失,n.labels 本身(参见下面的图 2)以及两个 x-月份(1 月至 12 月)的轴标签消失(见图 3)。

如果有人能够帮助在此图上放置正确的 n.label 值(请参阅下面的真实值),我将不胜感激。

非常感谢。

键:

n.label = 一个逻辑值,指示是否应将给出每个组中观察值的文本添加到图中。

 ##Three instances of each month over 3 years
 Year     Month       FID                             Month       FID  
 2018    January       86                             January     208
 2019    January       66                             February    176
 2020    January       56
 2018    February      76
 2019    February      55
 2020    February      45

 January (n=3)
 February (n=3) etc...............

三年内每个月的正确观察次数(见下文):

##the correct n.labels are these observations

       Month Observations 
1    January       113  
2   February        94  
3      March       111  
4      April       111  
5        May        33  
6       June         9   
7       July        14   
8     August        89  
9  September        86  
10   October        83  
11  November        81  
12  December       101  

R-代码:

      library(gplots)
      library(tidyverse)

    ##Produce a vector showing the true n.label value of observations
        
     Observations<-c(113, 94, 111, 111, 33, 9, 14, 89, 86, 83, 81, 101)

    ##Create a vector to ensure the dates are in the right order

     month_levels = c('January', 'February', 'March', 'April', 'May', 'June', 
                 'July', 'August', 'September', 'October', 'November', 'December')
    
    ##Plot means, ci_labels, and n.lables for the column 'FID'

    ##Open plotting window
     dev.new()
    
    ##Plot the mean per month for FID but with incorrect n.label values
    ##Code for figure 1

           plotmeans(FID~Month, 
                    data=FID,
                    ci.label = TRUE,
                    mean.labels = TRUE,
                    n.label = TRUE,
                    digits = 2,
                    pch=0.3, 
                    col="red",
                    ccol="black",
                    barcol="blue",
                    ylab="Mean Blue Whale Sightings",
                    xlab="Months")

          ##Open plotting window
            dev.new()

          ##Code for figure 2

          plotmeans(FID~Month, 
                    data=FID,
                    ci.label = TRUE,
                    mean.labels = TRUE,
                    n.label = paste0("month_levels", levels=Observations),
                    digits = 2,
                    pch=0.3, 
                    col="red",
                    ccol="black",
                    barcol="blue",
                    ylab="FID",
                    xlab="Months")

       ##Plot means for the 'Final_New_Blue'
       ##Open plotting window
         dev.new(width=10, height=10, unit="in")

       ## Margins area
         par(oma=c(3,3,3,3)) # all sides have 3 lines of space

          Obs <-c(111, 33, 9, 14, 89, 86, 83, 81, 101, 113, 94, 111)
           
               plotmeans(FID~Month, 
                         data=FID,
                         ci.label = TRUE,
                         mean.labels = TRUE,
                         n.label = FALSE,
                         digits = 2,
                         pch=0.3, 
                         col="red",
                         ccol="black",
                         barcol="blue",
                         ylab="FID",
                         xlab="Months")
           
                         axis(1, at=1:12, labels = paste("n =", Obs), pos = -35, col = NA)
           
           

图一

图2

图3

数据框:FID

structure(list(Year = c(2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L), Month = structure(c(5L, 4L, 8L, 1L, 9L, 
7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 
12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 
10L, 3L), .Label = c("April", "August", "December", "February", 
"January", "July", "June", "March", "May", "November", "October", 
"September"), class = "factor"), FID = c(65L, 88L, 43L, 54L, 
98L, 0L, 0L, 23L, 10L, 15L, 6L, 33L, 56L, 29L, 98L, 23L, 6L, 
10L, 7L, 65L, 53L, 41L, 25L, 30L, 44L, 65L, 38L, 27L, 20L, 0L, 
8L, 45L, 34L, 26L, 44L, 39L)), class = "data.frame", row.names = c(NA, 
-36L))

首先,一些坏消息:plotmeans() 可能不是您正在做的事情的最佳功能。问题是 n.label 只是一个 true/false 值,它决定了绘图是否将观测值的数量相加并将它们添加到轴上。 plotmeans() 不允许您更改该值,除非您编辑函数的代码,这需要时间。

现在有个好消息:可以绕过这个限制并手动修复你的情节。首先,将 n.label 设置为 FALSE:

# Rank factor levels by month name
FID$Month <- factor(FID$Month, levels = month.name)

##Code for figure 2
dev.new()
plotmeans(FID~Month, 
          data=FID,
          ci.label = TRUE,
          mean.labels = TRUE,
          n.label = FALSE,
          digits = 2,
          pch=0.3, 
          col="red",
          ccol="black",
          barcol="blue",
          ylab="FID",
          xlab="Months")

现在您可以使用 Base R 函数 axis():

手动添加高于 x-axis 的观察次数
Obs <-c(111, 33, 9, 14, 89, 86, 83, 81, 101, 113, 94, 111)
axis(1, at=1:12, labels = paste("n =", Obs), pos = -70, col = NA)

要调整标签的位置,请更改 pos = 的值。

结果: