Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to create percent stacked shape with ggplot

$
0
0

is there a way that I could do Percent stacked plot with ggplot2geom_rect() layer? I have coordinates, so I create a shape, I also have my separation variable t, now I would like to fill the shape based on the occurrence of t. In other words, I would like to do Percent stacked barchart but instead of a barchart a custom shape (in my case, simple rectangles). I've been experimenting a lot with position="fill", stat="identity" and their varieties but without any success. I wonder if it possible and if so, how could I achieve it?

df <- structure(list(x1 = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 
3, 3), x2 = c(6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 4), y1 = c(3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1), y2 = c(5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 2, 2, 2, 2, 2), t = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L), .Label = c("a", 
"b"), class = "factor")), row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15"), class = "data.frame")

library(ggplot2)
ggplot() + 
    geom_rect(data=d, mapping=aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2, fill = t))

expected result enter image description here

Thank you for your time!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>