2012-11-08 45 views
-9

我需要在R製作一張barplot。
基本上我有一個棒球運動員的數據集,列出了每個運動員所在的隊伍以及每個運動員的位置。例如:製作一張Barplot

Player Team Position 
1 Diamondbacks First Base 
2 Diamondbacks Third Base 
3 White Sox Left Field 
4 Giants  Pitcher 

實際的數據集比這個大得多,但它的想法相同。我需要製作一張展示團隊中不同職位頻率的barplot,我不知道如何去做。基本上,我所知道的是barplot(),所以任何幫助都會很棒。

謝謝!

+2

請提供一些示例數據。您可能還會發現閱讀[關於良好'R'問題的這個問題]很有價值(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 – metasequoia

回答

1

考慮一個分組條形圖。

修改例如從this question

# if you haven't installed ggplot, if yes leave this line out 
install.packages("ggplot2") # choose your favorite mirror 

require(ggplot2) 
data(diamonds) # your data here instead 
# check the dataset 
head(diamonds) 
# plot it, your team variable replaces 'clarity' and field position replaces 'cut' 
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge") + 
opts(title="Examplary Grouped Barplot") 
0

barplot()效果很好,如果你給它一個表。請看下面的數據:

set.seed(423) 
data <- data.frame(player = 1:100, 
        team  = sample(c("Team1", "Team2", "Team3"), 100, replace = TRUE), 
        position = sample(c("Pos1", "Pos2", "Pos3", "Pos4"), 100, replace = TRUE)) 

首先,讓我們做一個二維表:

tab <- table(data$team, data$position) 

一個barplot你可以通過tab定義的配置使data會是這樣:

barplot(tab, beside = TRUE, legend = TRUE) 

它給你以下內容: enter image description here

您可以運行?barplot以瞭解如何進一步自定義您的繪圖。

相關問題