-9

我需要在 R 中制作一个条形图。
基本上我有一个棒球运动员数据集,其中列出了每个球员所在的球队以及每个球员的位置。例如:

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

实际的数据集比这大得多,但它的想法是一样的。我需要制作一个条形图来显示团队中不同位置的频率,我不知道该怎么做。基本上,我所知道的是barplot(),所以任何帮助都会很棒。

谢谢!

4

2 回答 2

2

考虑一个分组条形图。

此问题的修改示例

# 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")
于 2012-11-08T03:25:14.373 回答
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)

您可以data使用定义的处置制作的一个条形图tab是这样的:

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

这为您提供了以下内容: 在此处输入图像描述

您可以运行?barplot以了解如何进一步自定义您的情节。

于 2014-10-15T22:03:22.707 回答