Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

r - How to always plot Sum as the last label on x axis and bottom row on the y axis

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a solution that addresses both axes and puts a grand total at the intersection of the "total" row and "total" column:

  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")) +
    geom_text(aes(label = Count), 
      data = data.frame(Count = sum(myplot$Count), V2 = "Total", z = "Total")) +
    scale_x_discrete(limits = c(unique(myplot$V2), "Total")) +
    scale_y_discrete(limits = rev(c(unique(myplot$z), "Total")))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...