r - ggplot2 - Stacked bar with different width -


i want produce reverse pyramid graph bar stacked on each other different width.

1st, have stacked bar chart below code sample

library(dplyr) library(ggplot2) sample <- data_frame(x=c(1, 1, 1, 1, 2, 2, 2, 2),                      y=c(5,10,15, 20, 10, 5, 20, 10),                      w=c(1, 2, 3, 4, 1, 2, 3, 4),                      group=c("a", "b", "c", "d", "a", "b", "c", "d"))  ggplot() +     geom_bar(data=sample,              aes(x=x,y=y,group=group, fill=group),              stat="identity", position=position_stack()) 

enter image description here

then added width aes 1 lower w value smaller while still stacked on each other. however, bars didn't stack warnings.

ggplot() + geom_bar(data=sample,          aes(x=x,y=y,group=group, fill=group, width=w/5),          stat="identity", position=position_stack()) 

enter image description here

warning: ignoring unknown aesthetics: width warning message: position_stack requires non-overlapping x intervals  

any helps on make bar plot stacked or ideas on different plot type can cover similar concepts highly appreciated. thanks!

it's bit of hack.

i'll use geom_rect() instead of real columns. such need create data.frame() precalculated positions rectangle boundaries.

df_plot <- sample %>%    arrange(desc(group)) %>% # order lowest 'groups' firtst   group_by(x) %>%    mutate(yc = cumsum(y), # calculate position of "top" every rectangle          yc2 = lag(yc, default = 0) ,# , position of "bottom"          w2 = w/5) # small scale width   # plot  ggplot(df_plot) +   geom_rect(     aes(xmin = x - w2 / 2, xmax = x + w2 / 2,         ymin = yc, ymax = yc2,         group = group, fill=group)) 

resulting plot: enter image description here


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -