[英]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.