Alice Hobbs
Alice Hobbs

Reputation: 1215

Label the lower and upper confidence interval bars with numerical values using geom_text()

Problem

I produced plot 1 (see R code below) using the package ggplot() and my ultimate goal is to label both the upper and lower confidence interval bars with their values to 3 significant figures (stated in the table below) on the plot by aligning these labels with the upper and lower error bars (see plot 3).

I attempted to manipulate the code (plot 2) using geom_text() (see R code below); however, instead of labelling either confidence interval bar, I accidentally placed erroneous numerical labels (that are not in my data table) on each mean month point per month. When I ran the code for plot 2, the background grid and background colour also came back, and I would prefer to keep the background in my plot blank (plots 1 + 3).

Desired output

I would like to produce a plot that resembles plot 3 (see below) by labelling the associated ci_low and ci_high values within the table in the same fashion.

If anyone can help me resolve this issue, I would be deeply appreciative.

Thank you :)

Table Key:

Table:

Merged_Blue_Whale_Summarised <- read.table(text = "
       Month Counts Mean.Month  sd.Month       S.E    ci_low    ci_hi
1    January    113   37.66667  5.686241  3.282953 31.232080 44.10125
2   February     94   31.33333  4.932883  2.848001 25.751251 36.91542
3      March    111   37.00000  5.291503  3.055050 31.012101 42.98790
4      April    111   37.00000 12.288206  7.094599 23.094586 50.90541
5        May     33   11.00000  7.937254  4.582576  2.018152 19.98185
6       July     16    8.00000  1.414214  1.000000  6.040000  9.96000
7     August     89   29.66667  9.291573  5.364492 19.152262 40.18107
8  September     86   28.66667 16.441817  9.492687 10.061000 47.27233
9    October     82   27.33333 12.503333  7.218803 13.184480 41.48219
10  November     81   27.00000 17.691806 10.214369  6.979837 47.02016
11  December    101   33.66667  4.041452  2.333333 29.093333 38.24000", header = TRUE)
# fix the month names order
Merged_Blue_Whale_Summarised$Month <- factor(Merged_Blue_Whale_Summarised$Month, levels = month.name)

R-code

library(ggplot2)

##Open a new window to plot the figure showing mean, and confidence intervals per month
dev.new()

####Code for plot 1

           p = ggplot(Merged_Blue_Whale_Summarised, aes(x=Month, y=Mean.Month, ymin=ci_low, ymax=ci_hi)) +
                      geom_line(aes(group=1), size=1) +
                      geom_errorbar(width=0.2, color="blue") + 
                      geom_point(size=2) + 
                      geom_label(aes(y=60, label=paste0("n=", Counts))) 
           
           p + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                          panel.background = element_blank(), axis.line = element_line(colour = "black"))
           
           ggsave("p.png", p)

p

##Code for plot 2

 p = ggplot(Merged_Blue_Whale_Summarised, aes(x=Month, y=Mean.Month, ymin=ci_low, ymax=ci_hi)) +
                  geom_line(aes(group=1), size=1) +
                  geom_errorbar(width=0.2, color="blue") + 
                  geom_point(size=2) + 
                  geom_label(aes(y=60, label=paste0("n=", Counts))) 
       
       p + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                      panel.background = element_blank(), axis.line = element_line(colour = "black"))
       
       p + geom_text(aes(label= sprintf("%.1f", ci_low, ci_hi), vjust=1.6,
                         color="black",
                         size=3.5))

       p

plot 1

enter image description here

Plot 2

enter image description here

Plot 3

enter image description here

Upvotes: 5

Views: 977

Answers (1)

zx8754
zx8754

Reputation: 56219

We will need to call geom_text twice, try:

ggplot(Merged_Blue_Whale_Summarised, aes(x=Month, y=Mean.Month, ymin=ci_low, ymax=ci_hi)) +
  geom_line(aes(group=1), size=1) +
  geom_errorbar(width=0.2, color="blue") + 
  geom_point(size=2) + 
  geom_label(aes(y=60, label=paste0("n=", Counts))) +
  # add high values
  geom_text(data = Merged_Blue_Whale_Summarised,
            aes(x = Month, y = ci_hi, label= sprintf("%.1f", ci_hi), vjust=-1.6)) +
  # add low values
  geom_text(data = Merged_Blue_Whale_Summarised,
            aes(x = Month, y = ci_low, label= sprintf("%.1f", ci_low), vjust=1.6)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))

enter image description here

Upvotes: 3

Related Questions