[英]Making a Barplot
我需要在 R 中制作一個條形圖。
基本上,我有一個棒球運動員數據集,其中列出了每個球員所在的球隊以及每個球員所在的位置。 例如:
Player Team Position
1 Diamondbacks First Base
2 Diamondbacks Third Base
3 White Sox Left Field
4 Giants Pitcher
實際的數據集比這個大得多,但它的想法是一樣的。 我需要制作一個條形圖,顯示團隊中不同職位的頻率,但我不知道該怎么做。 基本上,我只知道barplot()
,所以任何幫助都會很棒。
謝謝!
考慮一個分組條形圖。
來自這個問題的修改示例
# 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")
如果你給它一張桌子, 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)
您可以使用tab
定義的配置制作data
一個條形圖是這樣的:
barplot(tab, beside = TRUE, legend = TRUE)
這為您提供了以下內容:
您可以運行?barplot
以了解如何進一步自定義您的繪圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.