简体   繁体   English

在 R 中,如何使用分类 x 和带有 ggplot2 的数字 y 的平均值创建条形图?

[英]In R, how do I create a bar graph using a categorical x and the average of a numeric y with ggplot2?

I am using R version 3.5.1.我正在使用 R 版本 3.5.1。

I started with this problem , and used the code suggested by the top comment on my own dataset, where time_len is a categorical variable telling how long it takes to play game i, with values of short, medium, and long;我从这个问题开始,并在我自己的数据集上使用了顶部评论建议的代码,其中time_len是一个分类变量,表示玩游戏 i 需要多长时间,值分别为 short、medium 和 long; num_fans is a numeric variable telling how many fans game i has: num_fans是一个数字变量,表示我有多少粉丝游戏:

ggplot(cdata) +
  aes(x = time_len, y = num_fans) +
  geom_bar(stat = "identity")

Here is the plot:这是情节:

数据条形图

I created the bar chart using the code above, but the problem is that the num_fans variable looks like it is a total.我使用上面的代码创建了条形图,但问题是num_fans变量看起来像是一个总数。 I want it to show the average for each category.我希望它显示每个类别的平均值。

EDIT: here is a sample of my data.编辑:这是我的数据示例。

  game_id min_players single_player max_players family_game avg_time time_len year life avg_rating
1  161936           2         multi           4      family       60    short 2015    4    8.65105
2  187645           2         multi           4      family      240     long 2016    3    8.45420
3   12333           2         multi           2       small      180     long 2005   14    8.32843
4  193738           2         multi           4      family      150   medium 2016    3    8.28646
5  162886           1        single           4      family      120   medium 2017    2    8.39401
6   84876           2         multi           4      family       90   medium 2011    8    8.12803
  geek_rating   rating age owned num_fans
1     8.49375 8.572400  13 47498     2168
2     8.16391 8.309055  14 23989     1594
3     8.18051 8.254470  13 45955     3639
4     8.07144 8.178950  12 21513      835
5     7.96162 8.177815  13 13743     1002
6     8.01120 8.069615  12 49353     1866

Again, I am only asking about time_len.同样,我只询问 time_len。 Is there a way to show the average of num_fans ?有没有办法显示num_fans的平均值?

Try replacing the尝试更换

geom_bar(stat = "identity")

line with符合

stat_summary(geom = "bar", fun.y = "mean")?

(Note: this is the answer contributed by Z.Lin, who told me that I could post it as an answer.) (注意:这是 Z.Lin 提供的答案,他告诉我可以将其发布为答案。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ggplot2在R中重新排列此条形图? - How do I reorder this bar plot in R using ggplot2? 使用 R 和 ggplot2 如何创建具有多列的图形 - Using R and ggplot2 how do I create a graph with multiple columns 如何从 ggplot2 中的两个分类变量创建 x 轴标签? - How do I create x-axis labels from two categorical variables in ggplot2? R:GGPLOT2:如何使用 stat_summary 在 ggplot2 的折线图中添加标签 - R: GGPLOT2: How do I add labels in a line graph in ggplot2 using stat_summary 如何在R ggplot中创建具有不同离散X轴的多面条形图 - How do I Create a Faceted Bar Graph with Different Discrete X Axis in R ggplot 如何使用 R 中的 ggplot 减小分类 x 变量的折线图上的 x 轴大小? - How do you reduce the xaxis size on a line graph for a categorical x-variable using ggplot in R? R:在ggplot2中使用条形图中的日期 - R: using dates in bar graph with ggplot2 使用R中的ggplot2进行时序图时,如何在x轴上正确缩放日期? - How do I scale dates correctly on x-axys when doing time-series graph using ggplot2 in R? 如何使用 ggplot2 为每列创建条形图? - How to create a bar graph for each column using ggplot2? 如何使用ggplot2绘制带有(x,y,r,g,b)坐标的图像? - How can I plot a image with (x,y,r,g,b) coordinates using ggplot2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM