I want to create a plot with two continuous variables (v1 and v2) and one categorical variable (ex levels A,B,C,D). The plot should show a matrix of proportions. The categorical variable should be on the x-axis and each column should have two boxes (v1 and v2) representing the proportion of each continuous variable within that category (Within A, v1/(v1+v2) then v2/(v1+v2)). The width of the columns should represent the proportion of the total that is within that category (v1+v2 for A divided by the sum of all v1 and v2)
It should look like a heatmap but with the variable type (v1 or v2) mapped to color and the height and width of the boxes mapped as described above.
Using a stacked bar graph approach worked well and is close to what I want but there is horizontal space between the bars. Since I'm already using the width aesthetic to map the proportion within each category I wasn't able to eliminate this space.
Alternatively I tried to use geom_tile but that suffered from the same space issue and didn't result in all bars with a height of 1.
The closest solution I have found is: ggplot2 heatmap with tile height and width as aes()
However in that example they have a categorical variable on both X and Y axes which is a little different than my case.
Reproducible example for reference:
library(tidyverse)
cat <- c("A","B","C","D")
v1 <- c(1,3,6,2)
v2 <- c(3,3,10,1)
df <- data.frame(cat,v1,v2)
df <- df %>%
group_by(cat) %>%
mutate(sum.cat = sum(v1,v2)) %>%
mutate(prop.v1 = v1/sum.cat) %>%
ungroup() %>%
mutate(prop.cat = sum.cat/sum(v1,v2)) %>%
mutate(sum.tot = sum(sum.cat)) %>%
mutate(prop.v2 = 1-prop.v1) %>%
pivot_longer(cols = c(5,8), names_to = "prop.v.type", values_to = "prop.v")
ggplot(df,aes(cat,prop.v, fill = prop.v.type))+
geom_bar(position = "stack", stat = "identity",aes(width=prop.cat))
ggplot(df,aes(x=cat, y=prop.v, fill = prop.v.type))+
geom_tile(aes(width=prop.cat,height=prop.v))
Thanks in advance!