0

I have the following data:

Sp  Type    Val1    Val2
A   One     200     50
A   Two     100     10
C   One     300     150
C   Two     200     10

I did the following to get stacked barplot:

ggplot() +
  geom_bar(data=test, aes(y = Val1, x = Sp, fill = Type), stat="identity",position='stack')

Hence, I get two stacked bars for A, B each with stacks of Type 1 & 2 (total size of A is 200+100 =300). Here, val2 is a fraction of unknowns in each Type. How do I overlay it in respective portions of stack? i.e in type A in Val1, fraction of unknown is Val2.

Thanks in Advance.

AP

Arun
  • 649
  • 8
  • 24
  • Please be more precise. Do you want to overlay `Val2` above `Val1`. Or do you want a stacked barplot including both values? – Roman Aug 02 '17 at 12:58
  • It is stacked barplot with Val2 shaded. I mean I want to add the Val2 inside Val1 bar. i.e A will have 2 stacks, 1 with 200, 2 with 100. In stack with 200, I want to shade a region with 50 and 100 with 10. It will be better, if we have 50 & 10 with same color so that, it would look like proportion marked in both stacks !. – Arun Aug 03 '17 at 14:00
  • ok, then have a look at my answer! The others only summing up Val1 and Val2. – Roman Aug 04 '17 at 06:00

3 Answers3

2

Is this what You are looking for?:

library(ggplot2)
data <- data.frame(Sp  = c("A","A","C","C"), 
                   Type = c("one","two","one","two"), 
                   Val1 = c(200,100,300,200),
                   Val2 = c(50,10,150,10))
library(reshape2)
data <- melt(data, id=c("Sp","Type"))
data$Type2 <- paste(data$Type, data$variable, sep="_")

[UPDATE] The data which you get after melting:

 Sp Type variable value    Type2
1  A  one     Val1   200 one_Val1 # it has value of 200 for Sp A
2  A  two     Val1   100 two_Val1 # it has value of 100 for Sp A
3  C  one     Val1   300 one_Val1
4  C  two     Val1   200 two_Val1
5  A  one     Val2    50 one_Val2
6  A  two     Val2    10 two_Val2
7  C  one     Val2   150 one_Val2
8  C  two     Val2    10 two_Val2

whereas one_Val1 is equal to 200 and two_Val1 to 100 --> 200 + 100 = 300

ggplot() +
  geom_bar(data=data, aes(y = value, x = Sp, fill = Type2), stat="identity",position='stack')

enter image description here

I have melted at first your data to get values of Val1 and Val2 in one column to use it further and paste it together with the Type column.

Mal_a
  • 3,670
  • 1
  • 27
  • 60
1

In case you wanted it divided per Val1/Val2 values

library(ggplot2)
library(reshape2)
test <- melt(test, id=c("Sp","Type"))
ggplot(data=test) +
  geom_bar(aes(y = value, x = Sp, fill = Type), stat="identity",position='stack')+
  facet_wrap(~variable)
Patrik_P
  • 3,066
  • 3
  • 22
  • 39
1

You can try:

d$xmin <- rep(c(0.55, 1.55),each=2)
d$xmax <- rep(c(1.45, 2.45),each=2)
d$ymin <- c(100, 0, 200, 0)
d$ymax <- c(150, 10, 350, 10)

ggplot(d) + 
    geom_col(aes(x=Sp, y=Val1, fill=Type)) +
    geom_rect(aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), alpha=0.5) 

The idea is to manually add rectangles over the bars (I'm using here geom_col as this function uses stat_identity as default). Thus you calculate the mins and max by yourself and add some alpha to overplot the bars.

Or you can try a more automatic dplyr solution:

library(tidyverse)
d %>% 
  arrange(Sp, -as.numeric(Type)) %>% 
  mutate(ymin=ifelse(Type=="One",lag(Val1),0),
         ymax=ifelse(Type=="Two",Val2, lag(Val1)+Val2)) %>% 
  mutate(Sp_n=as.numeric(Sp)) %>% 
  ggplot() + 
  geom_col(aes(x=Sp_n, y=Val1, fill=Type))+
  geom_rect(aes(xmin=Sp_n-0.45, xmax=Sp_n+0.45, ymin=ymin, ymax=ymax),
  fill="white", alpha= 0.7) +
  scale_x_continuous(breaks = 1:2, labels = unique(d$Sp))

enter image description here

Roman
  • 17,008
  • 3
  • 36
  • 49