I am using ggplot to plot my data as below. My current code places total alphabetically anywhere in the plot. However I want the aggregate of rows to always be plotted at the first row from the bottom of the plot and aggregate of columns at the very last.
Sample data:
mydata <- structure(list(V1 = structure(c(1L, 12L, 23L, 34L, 45L, 56L,
57L, 58L, 59L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L
), .Label = c("A1", "A10", "A11", "A12", "A13", "A14", "A15",
"A16", "A17", "A18", "A19", "A2", "A20", "A21", "A22", "A23",
"A24", "A25", "A26", "A27", "A28", "A29", "A3", "A30", "A31",
"A32", "A33", "A34", "A35", "A36", "A37", "A38", "A39", "A4",
"A40", "A41", "A42", "A43", "A44", "A45", "A46", "A47", "A48",
"A49", "A5", "A50", "A51", "A52", "A53", "A54", "A55", "A56",
"A57", "A58", "A59", "A6", "A7", "A8", "A9"), class = "factor"),
V2 = structure(c(1L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L,
1L, 1L, 3L, 1L), .Label = c("AA", "AAAA", "YY"), class = "factor"),
V3 = structure(c(4L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 4L, 1L, 1L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
1L, 1L, 1L, 1L, 1L, 2L, 4L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L,
1L, 1L, 4L, 1L), .Label = c("AA", "AAAA", "bb", "ll"), class = "factor"),
V4 = structure(c(3L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 4L, 3L, 1L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
3L, 1L, 3L, 3L, 3L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 3L,
1L, 1L, 3L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L,
1L, 1L, 4L, 1L), .Label = c("AA", "AAAA", "bb", "ll"), class = "factor"),
V5 = structure(c(1L, 3L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 3L,
3L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 1L, 3L, 1L,
3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L,
2L, 1L, 4L, 3L), .Label = c("AA", "AAbb", "bb", "ll"), class = "factor"),
V6 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L), .Label = c("bb", "ll"), class = "factor"),
V7 = structure(c(1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 1L, 3L, 1L, 1L, 2L,
1L, 1L, 3L, 1L), .Label = c("AA", "AAbb", "bb", "bbAA"), class = "factor")), class = "data.frame", row.names = c(NA,
-59L))
Code:
library(ggplot2)
library(dplyr)
mydata$z=paste(mydata$V2,mydata$V3,mydata$V4, mydata$V5, mydata$V6,mydata$V7, sep="")
myplot <- mydata %>% group_by(V2, z) %>%
summarize(Count = n())
ggplot(myplot, aes(V2, z)) + geom_tile(aes(fill=Count), width=0.3) + geom_text(aes(label = Count), cbind(aggregate(Count~z, myplot, sum), V2="Total")) + geom_text(aes(label = Count) ,cbind(aggregate(Count~V2, myplot, sum), z="Total"))
question from:
https://stackoverflow.com/questions/65866142/how-to-always-plot-sum-as-the-last-label-on-x-axis-and-bottom-row-on-the-y-axis