1

Problem:

I have a data frame called FID and I want to label the number of observations for each mean and the upper and lower confidence intervals. Because the data frame shows the number of observations per month over three years (below), the n.labels are being labelled as n=3 (see figure 1 below).

I created two vectors called observations and month_level and I was hoping to use the function paste0() to insert the true n.label values into the plot (see R code). When I tried to paste these n.label values onto the plot, the plotline joining the mean values tend to disappear and so do the n.labels themselves (see figure 2 below), in conjunction with two of the x-axis labels for months (January-December) disappearing (see figure 3).

If anyone is able to help to place the correct n.label values (see the true values below) on this plot, I would be deeply appreciative.

Many thanks in advance.

Key:

n.label = a logical value indicating whether text giving the number of observations in each group should be added to the plot.

 ##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 number of observations per month over three years (viewed below):

##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-code:

      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)
           
           

Figure 1

enter image description here

Figure 2

enter image description here

Figure 3

enter image description here

Data frame: 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))
Alice Hobbs
  • 1,021
  • 1
  • 15
  • 31

1 Answers1

1

First, some bad news: plotmeans() might not be the best function to do what you're doing. The problem is that n.label is just a true/false value that determines whether the plot will sum the number of observations and add them to the axis. plotmeans() doesn't let you change that value unless you edit the function's code, which takes time.

Now for the good news: it is possible to get around this limitation and fix your plot manually. First, set n.label to 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")

Now you can manually add the number of observations above your x-axis using the Base R function 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)

To adjust the position of the labels, change the value of pos =.

Result: Alice's plot

Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
  • 1
    Thanks for the answer. If you'd like to propose a PR, feel free to do it here: https://github.com/talgalili/gplots And I'll take a look. T – Tal Galili Oct 01 '20 at 20:06
  • Hey Werner. Thank you so much for your reply, I really appreciated it. Could I please ask what I am doing wrong? I ran the code with the plotmean() function and the Obs labels are printing below the x-axis label, which you can only see in the plots global environment but not in a plotting window. Is there any way that the n=3 labels (above) can be manipulated to paste the values in the Obs vector instead in the same position within the plot? – Alice Hobbs Oct 02 '20 at 00:17
  • @AliceHobbs You're welcome! The error is in line `n.label = paste0("month_levels", levels=Observations)` (Figure 2 plot). `n.label` can only be `TRUE` or `FALSE`, it can't be a vector. No, there's no easy way to manipulate the n = 3 labels. Function `plotmeans()` doesn't have an argument that would allow you to change that, so you either have to do it manually or use a different data frame. Using the code I suggested, try using different `pos = ` values to fix the position of the manually added labels. – Werner Hertzog Oct 02 '20 at 00:37
  • Hey Werner. Great! Thank you for your guidance as I am not an advanced R user. I took your advice and changed the 'pos =' value. and I also changed the dimensions of the plotting window. Fantastic! You can now see the n.lablels on the plot. Sorry to ask more questions but a couple of my x-axis labels (January-December) have disappeared and the months are not in the right order. Is there any way I can fix this? I edited the question and put the new plot as figure 3 above. Thank you again if you can advise :) – Alice Hobbs Oct 02 '20 at 01:22
  • @AliceHobbs Are you sure the labels disappeared? Try expanding the plot window. You can sort the x-axis by month name by defining factor levels like this: `FID$Month <- factor(FID$Month, levels = month.name)`, I'll update my answer with this line of code. – Werner Hertzog Oct 02 '20 at 01:52
  • Hey Werner. Thank you very much. I applied your suggestions with resizing the window and defining the factor windows and it worked. I really learned a lot from this interaction. I really appreciated your help – Alice Hobbs Oct 02 '20 at 03:48