简体   繁体   English

如何将一个单词与 R 条形图标题中列表的所有元素连接起来?

[英]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.

相关问题 如何通过选择列表中除一个值之外的所有元素来对R中的列表进行子集化? - How do I subset a list in R by selecting all elements in a list except for one value? 如何从 R 上显示的 x 轴获取条形图的所有标签? - How do I get all my labels from x-axis shown on R for a barplot? 如何在R中的条形图中为图例添加标题 - How to add title to the legend in this barplot in R 如何在 r 中使用列表元素作为 plot 标题? - How to use list elements as plot title in r? 如何将列表的所有元素放入/保存到 R 中的一张 Excel 工作表中? - How to put/save all elements of a List into one Excel sheet in R? 如何根据 R 中 data.frame 中给出的数据获取列表中所有元素的索引? - How do I get the index of all elements in a list, according to data given in a data.frame in R? 如何使用三列在 R 中制作堆叠条形图,我想使用 barplot() 函数 - How do I make stacked barplot in R utilising three columns, I want to use the barplot() function 如何在R中创建具有累积标准偏差的条形图? - How do I create a barplot in R with a cumulative standard deviation? 如何在R中的代码中更改小节的尺寸? - How do I change the dimensions of a barplot in the code, in R? 如何在条形图上更正 R 中 y 轴的比例和顺序 - How do I correct the scale and order of the y axis in R on a barplot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM