简体   繁体   English

如何用ggplot绘制用矩阵函数生成的列联表?

[英]how to plot with ggplot a contingency table generated with a matrix function?

The columns represent the grade of response of the respondent and the rows are the representation of the groups of ages. 列代表受访者的回答等级,行代表年龄组。 The table was generated with a (matrix?), the goal is to (graph? O make graphic) how the different groups of ages behave with the responses 该表格是用(矩阵?)生成的,目标是(图形?使图形)不同年龄段的人如何应对

tabla<-matrix(c(0,  0,  0,  1,  0,  0,  
                   1,   0,  0,  0,  9,  0,  
                   9,   1,  1,  5,  22, 0,  
                   18,  1,  3,  1,  27, 1,
                   25,  7,  4,  6,  22, 3,
                   20,  2,  0,  0,  18, 1,
                   6,   2,  0,  2,  22, 0,  
                   2,   0,  1,  1,  0,  4,  
                   12,  0,  0,  5,  6,  0),ncol=6,byrow=TRUE)
colnames(tabla)<-c("No","is a problem","lite preblem","a moderate proble","Big problem","No respond")
rownames(tabla)<-c("16-24.5","24.5-33","33-41.5","41.5-50","50-58.5","58.5-67","67-75.5","75.5-84","No responde")

I think heatmap is a good choice. 我认为热图是一个不错的选择。 Here is a solution using the tidyverse package. 这是使用tidyverse软件包的解决方案。

library(tidyverse)

tabla2 <- tabla %>%
  as.data.frame() %>%
  rownames_to_column() %>%
  gather(Column, Value, -rowname)

ggplot(tabla2, aes(x = rowname, y = Column, fill = Value)) +
  geom_tile() +
  scale_fill_gradientn(name = "", colors = terrain.colors(10)) +
  scale_x_discrete(name = "") +
  scale_y_discrete(name = "")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM