[英]How do I concatenate one word with all elements of a list in R barplot title?
This is the code I'm currently running:这是我目前正在运行的代码:
n <- 7
N <- 52
r <- 13
reps <- 1000000
deck <- rep(c('h','d','c','s'), each = r)
diamonds <- rep(NA, length.out = reps)
pos <- sample(x = 1:52, size = 7, replace = FALSE)
for(i in 1:reps) {
hand <- sample(x = deck, replace = FALSE)[pos]
diamonds[i] <- sum(ifelse(hand == 'd', 1, 0))
}
barplot(table(diamonds), col = 'red', xlab = '# of diamonds',
ylab = paste('frequency out of',reps,'trials'),
main = paste('Positions:',pos[1],pos[2],pos[3],pos[4],
pos[5],pos[6],pos[7]))
What I'd really like is to be able to give a title to the barplot with something like the following我真正想要的是能够为条形图命名,如下所示
barplot(..., main = paste('Positions:',pos))
and have the title say "Positions: p1 p2 p3 p4 p5 p6 p7", where p1,p2,...,p7 are the elements of pos.并让标题说“Positions: p1 p2 p3 p4 p5 p6 p7”,其中 p1,p2,...,p7 是 pos 的元素。
For anyone that's interested, this code randomly chooses 7 positions from 52 and then counts the number of diamonds ('d') within those 7 positions after each shuffle of the deck for 1000000 shuffles.对于任何感兴趣的人,此代码从 52 个位置中随机选择 7 个位置,然后在每次洗牌后计算这 7 个位置中的菱形 ('d') 数量,共 1000000 次洗牌。 Then the empirical distribution of the number of diamonds within those 7 cards is plotted.
然后绘制这 7 张卡片中钻石数量的经验分布。
Use collapse
in paste
to collapse the multiple elements in a vector containing the base test and pos
,在
paste
中使用collapse
来折叠包含基本 test 和pos
的向量中的多个元素,
paste(c('Positions:', pos), collapse=" ")
Otherwise, when you paste "Positions:" to pos
you get the former recycled to the length of pos
.否则,当您将“Positions:”粘贴到
pos
时,您会将前者回收到pos
的长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.