Home > Enterprise >  Conditionally add error bars to a ggplot2 using data from the plot
Conditionally add error bars to a ggplot2 using data from the plot

Time:07-16

I am plotting a dot plot representing two variables from a dataframe (x_values0, a character vector and AF, numerical):

This is an example dataframe:

x_values0 AF  sdPopHetAF

pos1      50     12.3
pos2      4      43.6
pos3      67     20.3
pos4      8     1.2
pos5      36     13.7
pos6      78     24.5
p<-ggplot(data=chr1, aes(x=x_values0, y=AF)) 
    geom_point(aes(col=color), size=2)

I am trying to add error bars to the points with AF lower than a value (in this case 95) and higher than a value (in this case 10). My intention is that each error bar shows the value AF - the value of the corresponding row of another variable (sdPopHetAF).

In the example above, I would like to add errorbars to pos1, pos3, pos5 and pos6; and error bars should extend, for example, in the case of pos1 from 37.7 to 72.3.

I have tryed the following commands:

  1. In the first case, I get the following error and no plot
ymin<-function(af, sd){
  if(af > 10 && af < 95){
    af - sd
  } else {NA_real_}
}

ymax<-function(af, sd){
  if(af > 10 && af < 95){
    af   sd
  } else {NA_real_}
}



p   geom_errorbar(aes(y=AF, fun.min = ymin(AF, sdPopHetAF), fun.max = ymax(AF,sdPopHetAF)),width = 0.2)
Error in `check_required_aesthetics()`:
! geom_errorbar requires the following missing aesthetics: ymin and ymax or xmin and xmax
Run `rlang::last_error()` to see where the error occurred.
Warning message:
Ignoring unknown aesthetics: fun.min, fun.max

In the second case, it plots errro bars to all my dots, and additionally it plots always the same error bars.

p   geom_errorbar(ymin = ifelse(AF>10 && AF<95, AF - sdPopHetAF, 0), ymax = ifelse(AF>10 && AF<95, AF   sdPopHetAF, 0),width=0.4)

enter image description here

In the last case, it does not plot errorbars and I get a warning

p   stat_summary(fun.min = ymin(AF, sdPopHetAF), fun.max = ymax(AF, sdPopHetAF), geom = "errorbar")

enter image description here

Warning message: Computation failed in stat_summary(): Can't convert fun, a double vector, to a function. How can I solve this?

CodePudding user response:

Perhaps this?

ggplot(chr1, aes(x_values0, AF))  
  geom_point()  
  geom_errorbar(data = subset(chr1, AF > 10, AF < 95),
                aes(ymin = AF - sdPopHetAF,
                    ymax = AF   sdPopHetAF))

enter image description here

Data:

chr1 <- data.frame(
  stringsAsFactors = FALSE,
         x_values0 = c("pos1", "pos2", "pos3", "pos4", "pos5", "pos6"),
                AF = c(50L, 4L, 67L, 8L, 36L, 78L),
        sdPopHetAF = c(12.3, 43.6, 20.3, 1.2, 13.7, 24.5)
)

CodePudding user response:

The best way to handle this is to subset the data that is supplied to the geom_errorbar() layer:

set.seed(123)
library(ggplot2)

dat <- tibble::tibble(
  x = factor(1:10),
  y = rnorm(10),
  ymin = y - .2,
  ymax = y   .2
)

ggplot(dat)   
  aes(x = x, y = y, ymin = ymin, ymax = ymax)  
  geom_point()   
  geom_errorbar(
    data = dplyr::filter(dat, y < 0),
    width = 0
  )

Created on 2022-07-15 by the reprex package (v2.0.1)

  • Related