Draw 3x3 square grid in R

前端 未结 3 679
情深已故
情深已故 2021-02-10 12:38

I got a list of number (n=9) and would like to draw them out in a 3*3 square grid and each grid fill with corresponding number. How can I do this in R without installing additio

3条回答
  •  遇见更好的自我
    2021-02-10 13:20

    Here is a ggplot solution that was a little harder than I expected:

    # Setup the data
    m <- matrix(c(8,3,4,1,5,9,6,7,2), nrow=3, ncol=3)
    df <- expand.grid(x=1:ncol(m),y=1:nrow(m))
    df$val <- m[as.matrix(df[c('y','x')])]
    
    library(ggplot2)
    library(scales)
    ggplot(df, aes(x=x, y=y, label=val)) + 
      geom_tile(fill='transparent', colour = 'black') + 
      geom_text(size = 14) + 
      scale_y_reverse() +
      theme_classic() + 
      theme(axis.text  = element_blank(),
            panel.grid = element_blank(),
            axis.line  = element_blank(),
            axis.ticks = element_blank(),
            axis.title = element_blank())
    

    enter image description here

提交回复
热议问题