如何将频率表转换为堆积的百分比条形图

时间:2019-07-04 05:53:07

标签: rstudio

我想创建一个以R为基数的堆积百分比条形图。有没有办法使它自动化?

 #calculate frequencies     
 table(initial$mth1)
 table(initial$mth2) 
 table(initial$mth3) 
 table(initial$mth4) 
 table(initial$mth5) 
 table(initial$mth6) 
 table(initial$mth7)
 table(initial$mth8)

 initialmth <- structure(list( #had to round some nums below for 100% bar

 A = c(49, 24, 9, 10, 4), 
 B = c(11, 16, 23, 28, 18),
 C = c(35, 34, 17, 9, 1),
 D = c(6, 6, 15, 37, 32), 
 E = c(29, 37, 10, 12, 8),
 F = c(7, 12, 15, 36, 26),
 G = c(5, 4, 9, 29, 49),
 H = c(4, 5, 18, 30, 39 )),

.Names = c("Math has been my worst subject ",
         "I would consider a career that uses math ",
         "Math is hard for me",
         "I am the type of student to do well in math",
         "I cannot do a good job with math",
         "I could do advanced work in math",
         "I can get good grades in math",
         "I am good at math"
), 
 class = "data.frame", 
 row.names = c(NA, -5L)) #4L=number of numbers in each letter vector#

 attach(initialmth)
 print(initialmth)


 # barplot
 colors <- c("slategray1", "slategray3","steelblue3", "dodgerblue4")
 par(mar=c(0, 17, 1, 2.1)) # this sets margins to allow long labels
 byc <- barplot(as.matrix(initialmth, horiz=TRUE, col=colors, main="N=96", 

           border=FALSE, las=1, xaxt='n',
       ylim = range(0, 12), xlim = range(0, 100)))

我想将上面计算出的频率转换成百分比堆叠的条形图。有没有办法在基础R中自动执行此过程?

0 个答案:

没有答案