Wednesday, April 17, 2013

RG#76: Barplot with both X and Y quantitative value (width and height bar plot)


# data 
dff <- data.frame(x = c("Aa", "Bbb", "Ccc", "Dddd", "Eeeee"),
xvr = c(35, 50, 15, 85, 100), yvr = c(100, 75, 50, 45, 25))

# reformat data  
dff$w <- cumsum(dff$xvr)
dff$wm <- dff$w - dff$xvr
dff$wt <- with(dff, wm + (w - wm)/2)

library(ggplot2)
library(grid)

# plot 
p  <- ggplot(dff, aes(ymin = 0))
p1 <- p + geom_rect(aes(xmin = wm, xmax = w, ymax = yvr, colour = x, fill = x)) +
       scale_colour_manual(values = c("green4", "lightseagreen", "pink", "blue3", "tan")) +
        scale_fill_manual (values = c("green4", "lightseagreen", "pink", "blue3", "tan"))
       

p1 + geom_text(aes(x = wt, y = yvr * 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 = "Bbb") +
     annotate("text", x = 270, y = 35, label = "Eeeee") +
     geom_segment(aes(x = 100, y = 80, xend = 80, yend = 75),
     arrow = arrow(length = unit(0.5, "cm"))) +
         geom_segment(aes(x = 280, y = 32, xend = 250, yend = 25),
     arrow = arrow(length = unit(0.5, "cm"))) + theme_bw()




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.