ggplot带R的分组条形图 [英] ggplot Grouped barplot with R

查看:95
本文介绍了ggplot带R的分组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建分组的barplot时遇到困难.这是我第一次使用R中的散点图以外的其他任何东西.有团队,并且他们放弃了要靠位置的位置(代码下方).C,D和W.在r-graph-gallery中查找以下代码.

I am having difficulties creating a grouped barplot. This is my first time using anything other than the scatter plot in R. There is the Team, and the points they give up to the position(Below the Code). C, D, and W. Going off the r-graph-gallery for the code below.

library(ggplot2)
positions=rep(c("C","W","D"))

ggplot(data=stats, aes(fill=positions, y=0,200,x=PtsAgRdTm))+
  geom_bar(position = "dodge", stat = "indentity")

下面是 df 作为 stats 的数据,而不是转换为 data 的数据.

Below is the df as stats than converted to data.

PtsAgRdTm     C       D       W 
ANH         57.73   65.08   56.08
ARI         33      29.24   26.99
BOS         56.64   44.97   56.15
BUF         35.36   31.04   38.35
CAR         42.6    49.79   78.03
CLS         38.23   53.16   67.92
CGY         56.19   54.87   78.54
CHI         37.04   47.93   74.95
COL         54.87   47.83   78.22
DAL         59.05   39.67   33.3
DET         26.11   34.15   71.21
EDM         57.64   53.01   43.14
FLA         71.09   44.85   44.91
LA          53.06   48.62   42.11
MIN         41.86   51.44   51.93
MON         36.6    50      89.02
NJ          26.95   34.64   49.61
NSH         43.12   60.05   83.11
NYI         51.58   46.36   46.99
NYR         75.15   104.19  177.69
OTT         51.01   64.75   75.05
PHI         65.96   54.69   40.56
PIT         42.67   38.08   52.33
SJ          70.83   56.66   44.31
STL         40.51   58.83   81.85
TB          68      50.93   58.22
TOR         71.28   42.17   21.5
VAN         29.81   28.79   41.4
VGK         40.09   43.63   63.49
WPG         49.66   48.09   77.08
WAS         47.68   52.02   70.12

推荐答案

您可以尝试

library(tidyverse)
d %>% 
  gather(key, value, -PtsAgRdTm) %>% 
  ggplot(aes(x=PtsAgRdTm, y=value, fill=key)) +
   geom_col(position = "dodge")

您可以使用tidyr的collect函数将数据从宽转换为长,然后以躲避"或堆叠"的方式绘制条形图.

You transform the data from wide to long using tidyr's gather function, then plot the bars in a "dodge" or a "stack" way.

这篇关于ggplot带R的分组条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆