Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

ggplot2 - ggplot Grouped barplot with R

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")

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try

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

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...