【问题标题】:Barplot in R fill with certain valuesR中的条形图填充某些值
【发布时间】:2020-12-08 15:35:46
【问题描述】:

我有一个数据集,其中包含玩游戏的配对数据。我有一个条形图,显示了两人玩的总游戏数。但现在我希望那些栏('number')填满他们成功完成的游戏数量('sum')。我无法让它工作。条形图是这样创建的:

barplot(height = game_count$number, xlab = 'Pairs', ylim = c(0,35), ylab='Games played')

数据如下:

             participants sum number
1    06104873220647518670  30     32
2    06105747340637377404  23     24
3    06113978630633565020  28     32
4    06121794480617858550  25     27
5    06122613960611857952  23     26
6    06123139380653583516  25     28
7    06123650620648276595  28     32
8    06124453210624910109  32     34
9    06127993700610846968  24     26
10   06128440030639764541  19     24
11   06132461300624244572  26     30
12   06137611390651588167  25     28
13   06145014400637290807  16     19
14   06163181050611257617  30     30
15   06172024240651919112  21     23

【问题讨论】:

  • barplot(height = game_count$sum, xlab = 'Pairs', ylim = c(0,35), ylab='Games played')
  • Freq 列在哪里?请确保代码重现问题。另外,为了回答,我希望那些条('number')填满他们成功完成的游戏数量('sum'),为什么不交换number(或Freq ) 为sum?
  • 哦,是的,我编辑了这个问题。 “频率”应该是“数字”。好吧,因为我想在一个条形图中可视化他们成功完成的游戏数量与他们玩过的游戏总数的“总和”。

标签: r bar-chart


【解决方案1】:

一个选项可以是ggplot2:

library(ggplot2)
#Code
game_count$Freq <- game_count$sum/game_count$number
#Plot
ggplot(game_count,aes(x=1:nrow(game_count),y=Freq))+
  geom_col(fill='cyan3',color='black')+
  xlab('')

输出:

【讨论】:

    【解决方案2】:

    这对我有用:

    barplot(t(game_correct[c('number', 'sum')]), beside=TRUE, ylim=c(0,35), col=c('black', 'green'), main='Games played and successive games by the pairs', xlab='Pairs', ylab='Games')
    

    此图中的结果:

    【讨论】:

      猜你喜欢
      • 2020-08-20
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2021-01-04
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      相关资源
      最近更新 更多