【发布时间】:2021-02-22 22:23:46
【问题描述】:
我正在执行一个涉及对随机数求和的模拟。对于我的条形图,我想将中奖号码(数字 7、11、21)填充为红色,其余数字填充为白色。我的数据框中的颜色列准确地为值分配了颜色,但是当我生成条形图时,颜色显示不正确。我需要改变什么?
set.seed(1);
tickets = NULL;
simNum = 500;
for(i in 1:simNum){
tickets = rbind(tickets, c(sum(box = sample(1:13, 3, replace = TRUE)),
sum(box = sample(1:13, 3, replace = TRUE)), sum(box = sample(1:13, 3, replace = TRUE))));
}
tickets = data.frame(tickets);
#create data frame of all the sums
allSums = data.frame(c(tickets$sum1, tickets$sum2, tickets$sum3));
allSums$col = "white";
allSums$col[allSums[1] == 7 | allSums[1] == 11| allSums[1] == 21] = "red";
barplot(prop.table(table(allSums[1])), col = as.vector(allSums$col));
legend("right", legend = c("win", "lose"), fill = c("red", "white"));
【问题讨论】:
标签: r graph statistics bar-chart