How to create good quality bar composition graphics

Here are my details:

ind <- c( rep(1, 20), rep (2, 20), rep (3, 20), rep(4, 20)) bar <- c(rep(rep(1:4, each = 5), 4)) mark <- c(rep ("A", 5), rep("B", 5), rep("C",5), rep("D",5), rep("B", 20), rep("C", 20), "A", "B", "C", "A", "C","A", "D", "D", "D", "D","A", "C", "C", "A", "B", "B","B", "C","C","C") dataf <- data.frame(ind, bar, mark) 

Each individual (ind) has 4 bars in order and is colored. There should be more space between people. I can have any number of people. The next expected graph drawn in excel, I want to instead create a more beautiful graph in R.

enter image description here

Note that each ind level (1: 4) is equal to four bars (1: 4). All four bars for each index are grouped in one place.

+4
source share
2 answers

You can try using the rect function to individually build each rectangle. Try running a loop to iterate each window on the stack:

 par(mfrow=c(2, 2)) for(i in unique(ind)) { plot(0, col=0, bty="n", xaxt="n", yaxt="n", xlab = i, ylab = "", xlim=c(0, 5), ylim=c(-6, -1)) for(j in 1:4) { for(k in 1:5) { rect(j-0.4, -k-1, j+0.4, -k, col = which(unique(mark)==matrix(mark[ind==i], 5, 4)[k,j])+1, border = rgb(0,0,0,0)) text(j, -k-0.5, labels = matrix(mark[ind==i], 5, 4)[k,j]) } rect(j-0.4, -6, j+0.4, -1) } } 

enter image description here

+5
source

Here is one parameter using ggplot :

 dataf$y <- rep(5:1,times = 16) ggplot(dataf,aes(x=bar,y=y)) + facet_wrap(~ind) + geom_tile(aes(fill = mark),colour = "black") + geom_text(aes(label = mark)) + labs(x = NULL,y = NULL) + opts(axis.text.x = theme_blank(),axis.text.y = theme_blank()) 

enter image description here

Since I use geom_tile , you lose the separation between the "bars". The only other option is to use geom_rect , I think, and specify the coordinates for this will be more attractive.

+4
source

Source: https://habr.com/ru/post/1387471/


All Articles