2017-05-04 12 views
1

私は添付された絵に似たプロットを作ろうとしています。私は '統計'として 'カット'、 'チーム'として 'ダイヤモンドカラークラス'、 '値'として 'カウント'を除いています。私は、コードの長い部分でのデータフレームの設定が正しいバージョンだと思うが、私はまた、これを試してみました:R野球統計の積み上げ棒プロット

df <- read.table(textConnection(
    'Team Runs Doubles Triples Homers Walks 
    Redsox 878 343 25 208 558 
    Cubs 808 293 30 199 656 
    Phillies 610 231 35 161 424 
    Twins 722 288 35 200 513 
    Dodgers 725 272 21 189 525'), header = TRUE) 

この問題を修正するすべてのヘルプははるかに高く評価されるだろう。

df <- read.table(textConnection(
    'Stat Team Value 
    Runs Redsox 878 
    Runs Cubs 808 
    Runs Phillies 610 
    Runs Twins 722 
    Runs Dodgers 725 
    Doubles Redsox 343 
    Doubles Cubs 293 
    Doubles Phillies 231 
    Doubles Twins 288 
    Doubles Dodgers 272 
    Triples Redsox 25 
    Triples Cubs 30 
    Triples Phillies 35 
    Triples Twins 35 
    Triples Dodgers 21 
    Homers Redsox 208 
    Homers Cubs 199 
    Homers Phillies 161 
    Homers Twins 200 
    Homers Dodgers 189 
    Walks Redsox 558 
    Walks Cubs 656 
    Walks Phillies 424 
    Walks Twins 513 
    Walks Dodgers 525'), header = TRUE) 



library(ggplot2) 

hw <- theme_gray()+ theme(
    plot.title=element_text(hjust=0.5), 
    plot.subtitle=element_text(hjust=0.5), 
    plot.caption=element_text(hjust=-.5), 

    strip.text.y = element_blank(), 
    strip.background=element_rect(fill=rgb(.9,.95,1), 
           colour=gray(.5), size=.2), 

    panel.border=element_rect(fill=FALSE,colour=gray(.70)), 
    panel.grid.minor.y = element_blank(), 
    panel.grid.minor.x = element_blank(), 
    panel.spacing.x = unit(0.10,"cm"), 
    panel.spacing.y = unit(0.05,"cm"), 

    axis.ticks=element_blank(), 
    axis.text=element_text(colour="black"), 
    axis.text.y=element_text(margin=margin(0,3,0,3)), 
    axis.text.x=element_text(margin=margin(-1,0,3,0)) 
) 


ggplot(df,aes(x=Team,fill=Stat))+ 
    geom_bar(color=gray(.55)) + 
    labs(x="Team", 
     y="Value", 
     title="Baseball Team Stats (2016)", 
     fill="Stat")+ 
    scale_fill_manual(
    values=c("red","blue",rgb(0,.8,0),'cyan','violet'), 
    na.value="grey30")+ hw 

enter image description here

答えて

2

私は、これはあなたが探しているものであると信じています。私はあなたのポスト(長い1)の第二部でデータを使用:

ggplot(df,aes(x=Team, y = Value, fill=Stat))+ 
    geom_bar(color=gray(.55), stat = "identity") 

enter image description here

あなたは美的ystat = "identity"ので、それがスタックします追加する必要がありました。メモ変更を強調表示するために追加の書式をすべて削除しましたが、簡単に戻すことができます。

+0

ありがとうございました!私はy = Valueを試しましたが、stat = "identity"がないのでエラーが発生しました。 –

+0

問題ありません、喜んで助けてください! –

関連する問題