r - Constructing an area plot with outlines for discrete variable (i.e. with steps) -


similar geom_area plot areas , outlines ggplot, i'm trying construct stacked area plot outlines. since variables discrete i'm using geom_bar() stacking them. code follows:

require(ggplot2) require(reshape) x = 0:4 y1 = c(3,2,2,1,0) y2 = c(1,1,0,0,0) data = data.frame(x,y1,y2) data.plot <-melt(data, id.vars = "x") cols = c(y1="darkgrey",y2="lightgrey")  p = ggplot(data.plot,aes(x=x,y=value,fill=variable)) p +  geom_bar(aes(width=1),stat = "identity") + theme_bw() +  scale_fill_manual(values=cols)  

which gives stacked barplot

my problem adding outlines in example referred to. can use colour="black" in geom_bar() adds vertical lines between bars quite ugly.

does have suggestion these outlines? solution doesn't have based on geom_bar.

if possible, interested in solution dark grey part has outline, since outline has important interpretation. perhaps based on shifted version of geom_line()?

your plotting code (i don't want use c since that's function):

p <- ggplot(data.plot, aes(x = x, y = value, fill = variable)) p <- p + geom_bar(aes(width = 1), stat = "identity") + theme_bw() +  scale_fill_manual(values = cols) 

now add stepping line along bars:

p <- p + geom_step(aes(x = x - 0.5), position = "stack") 

it's bit more work fix line along axes:

library (dplyr) y.max <- data.plot %>% group_by(x) %>% summarize(s = sum(value)) y.max <- max(y.max$s)     p + geom_step(aes(x = x - 0.5, ymax = value), position = "stack") +        annotate('segment',                x = min(data.plot$x) - 0.5,                xend = min(data.plot$x) - 0.5,                y = 0,                yend = y.max) +       annotate('segment',                x = min(data.plot$x) - 0.5,                xend = max(data.plot$x) - 0.5,                y = 0,                yend = 0) 

plot

i'd interested in simpler solutions!


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -