Labeling of Bland Altman Plot

Viewed 47

I would like to label the LoA and the mean not only with values (above the dashed lines) but also with "+1.96 SD", "Mean", "-1.96 SD".

My current code looks like this:

HF_plot1 <- bland.altman.plot(MA_subset$HF_Uhr, 
                              MA_subset$HF_VNS, graph.sys = "ggplot2",
                              mode = 1)

HF_plot1$layers[[2]]$aes_params$colour <- c('blue', 'black', 'blue')

HF_plot1 + ggtitle("Bland-Altman plot for Heart Rate")+
  theme(plot.title = element_text(size=15, face = "bold", 
                                  hjust = 0.5)) +
  labs(x="Means of HR between Garmin Venu 2 & VNS analysis [bpm]
  (Garmin Venu 2 + VNS analysis)/2",
       y ="Difference of HR between 
       Garmin Venu 2 & VNS analysis [bpm]
(Garmin Venu 2- VNS analysis)")+
  theme(axis.title.x = element_text(size= 9, hjust = 0.5))+
  theme(axis.title.y = element_text(size= 8, hjust = 0.5, 
                                    lineheight= 1)) +
  geom_text(data = data.frame(y = HF_plot1$layers[[2]]$data$yintercept),
            aes(x = 100, y = y, label = round(y, 2)),
            hjust = 0, vjust = -0.4, size = 3)

enter image description here

Here is some data to reconstruct

dput(data) structure(list(HF_Uhr = c(66L, 62L, 62L, 63L, 70L, 62L, 69L, 62L, 60L, 62L, 68L, 61L, 72L, 74L, 79L, 55L, 71L, 70L, 85L, 84L, 64L, 69L, 69L, 74L, 79L, 63L, 77L, 79L, 85L, 73L), HF_VNS = c(65.3, 61.4, 60.8, 62.9, 69.4, 61.1, 68.4, 62.6, 59.5, 62.9, 68.4, 60.1, 78.2, 73.1, 78.8, 58.8, 68.5, 70.6, 84.4, 84.7, 61.3, 69.4, 67.4, 82.2, 72.8, 62.6, 77.4, 78.8, 87.2, 73.2)), row.names = c(NA, 30L), class = "data.frame")
1 Answers

We could do it this way:

change in your geom_text line to :

in data of geom_text add just a rownames_to_column() this will create a new column and these strings can be output via label = paste(rowname, round(y, 2)). We could change the text to whatever we want:

library(tibble)

 geom_text(data = data.frame(y = HF_plot1$layers[[2]]$data$yintercept) %>% 
              rownames_to_column(),
            aes(x = 100, y = y, label = paste(rowname, round(y, 2))),
            hjust = 0.8, vjust = -0.4, size = 3)

enter image description here

Related