Variable Width Bar Plot

后端 未结 2 1077
情深已故
情深已故 2020-12-14 03:11

I\'d like to produce an area/bar graph in R similar to this: \"Plot

2条回答
  •  执笔经年
    2020-12-14 03:47

    Inspired by the code from the blog post I mentioned above,

    df <- data.frame(x = c("Alpha", "Beta", "Gamma", "Delta"), width = c(25, 50, 75, 100), height = c(100, 75, 50, 25))
    df$w <- cumsum(df$width)
    df$wm <- df$w - df$width
    df$wt <- with(df, wm + (w - wm)/2)
    
    library(ggplot2)
    p  <- ggplot(df, aes(ymin = 0))
    p1 <- p + geom_rect(aes(xmin = wm, xmax = w, ymax = height, fill = x))
    library(grid) # needed for arrow function
    p1 + geom_text(aes(x = wt, y = height * 0.8, label = x)) + 
         theme_bw() + labs(x = NULL, y = NULL) + 
         theme(axis.ticks = element_blank(),axis.text.x = element_blank(), 
         axis.text.y = element_blank(), legend.position = "none") + 
         annotate("text", x = 120, y = 83, label = "a Beta block") + 
         geom_segment(aes(x = 100, y = 80, xend = 80, yend = 75), 
         arrow = arrow(length = unit(0.5, "cm")))
    

    beta blocker

提交回复
热议问题