Error bars for barcode in one direction only

Is it possible to configure errors in ggplot2 so that they appear in only one direction (for example, only up, but not down)?

df <- data.frame(trt = factor(c(1, 1, 2, 2)), resp = c(1, 5, 3, 4),
                 group = factor(c(1, 2, 1, 2)), se = c(0.1, 0.3, 0.3, 0.2))
df2 <- df[c(1,3), ]

limits <- aes(ymax = resp + se, ymin = resp - se)
dodge <- position_dodge(width = 0.9)

p <- ggplot(df, aes(fill = group, y = resp, x = trt))
p + geom_bar(position = dodge, stat = "identity") +
    geom_errorbar(limits, position = dodge, width = 0.25)

Rplot

+4
source share
4 answers

A simple approach is to first build error strings:

p +
  geom_errorbar(limits, position = dodge, width=0.25) +
  geom_bar(position = dodge, stat = "identity")

enter image description here

+9
source

Henrik's advice is excellent in this case, but I would suggest you take a look at where the upper and lower limits are set.

 limits <- aes(ymax = resp + se, ymin = resp - se)

On this line, you explicitly specify ggplot to set the bottom extension by setting ymin as resp - se; if you just set it as resp, then you will only have a top extension.

 limits <- aes(ymax = resp + se, ymin = resp)

... . , .

p <- ggplot(df, aes(fill = group, y = resp, x = trt))+
  geom_bar(position = dodge, stat = "identity") +
  geom_bar(position = dodge, stat = "identity", 
           color="black", show_guide=FALSE)+
  geom_errorbar(limits, position = dodge, width = 0.25)
p

bar plot

, , "" , . , .

+4

( ) , ggplot2 geom s.

geom_errorbar github ggplot2 .R . :

geom_uperrorbar <- function(mapping = NULL, data = NULL,
   stat = "identity", position = "identity",
   ...,
   na.rm = FALSE,
   show.legend = NA,
   inherit.aes = TRUE) {
   layer(
      data = data,
      mapping = mapping,
      stat = stat,
      geom = GeomUperrorbar,
      position = position,
      show.legend = show.legend,
      inherit.aes = inherit.aes,
      params = list(
         na.rm = na.rm,
         ...
      )
   )
}

geom_errorbar geom_uperrorbar geom = GeomErrorbar geom = GeomUperrorbar.

GeomUperrorbar <- ggproto("GeomUperrorbar", Geom,
   default_aes = aes(colour = "black", size = 0.5, linetype = 1, width = 0.5,
      alpha = NA),

   draw_key = draw_key_path,

, , . GeomErrorbar GeomUperrorbar.

   required_aes = c("x", "y", "ymax"),

   setup_data = function(data, params) {
      data$width <- data$width %||%
         params$width %||% (resolution(data$x, FALSE) * 0.9)

      transform(data,
         xmin = x - width / 2, xmax = x + width / 2, width = NULL
      )
   },

. x, y ymax, .. ymin y. y ( ymin), ymin, .

   draw_panel = function(data, panel_scales, coord, width = NULL) {
      GeomPath$draw_panel(data.frame(
         x = as.vector(rbind(data$xmin, data$xmax, NA, data$x,   data$x)),
         y = as.vector(rbind(data$ymax, data$ymax, NA, data$ymax, data$y)),
         colour = rep(data$colour, each = 5),
         alpha = rep(data$alpha, each = 5),
         size = rep(data$size, each = 5),
         linetype = rep(data$linetype, each = 5),
         group = rep(1:(nrow(data)), each = 5),
         stringsAsFactors = FALSE,
         row.names = 1:(nrow(data) * 5)
      ), panel_scales, coord)
   }
)

, x y, . , ymin y, , y, ymin.

"%||%" <- function(a, b) {
   if (!is.null(a)) a else b
}

- , , .

, geom_uperrorbar , geom_errorbar, geom = "uperrorbar" stat_summary, y ymin.

+1

Henriks , , . , , , "" ( ) , 0 y. , ymin 0.

, ( ), 0. , :

df <- data.frame(trt = factor(c(1, 1, 2, 2)), resp = c(1, 5, -3, 4),
             group = factor(c(1, 2, 1, 2)), se = c(2, 0.3, 4, 0.2))
df2 <- df[c(1,3), ]

limits <- aes(ymax = resp + se, ymin = resp - se)
dodge <- position_dodge(width = 0.9)

p <- ggplot(df, aes(fill = group, y = resp, x = trt))
p + geom_errorbar(limits, position = dodge, width = 0.25) +
    geom_bar(position = dodge, stat = "identity") 

, trt 1, 1 trt 2, group1.

, ifelse():

limits <- aes(ymax = ifelse(resp>0,resp + se,resp/2),
              ymin = ifelse(resp<0,resp - se,resp/2))

p + geom_errorbar(limits, position = dodge, width = 0.25) +
    geom_bar(position = dodge, stat = "identity") 

, , , , .

0

Source: https://habr.com/ru/post/1568484/


All Articles