-9

I need to make a barplot in R.
Basically I have a dataset of baseball players that lists what team each player is on and what position each player plays at. For example:

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

The actual dataset is much bigger than this, but its the same idea. I need to make a barplot of the showing the frequencies of the different positions in the teams, and I do not know how to go about doing so. Basically, all I know is barplot(), so any help would be great.

Thanks!

plannapus
  • 18,529
  • 4
  • 72
  • 94
Bob Kart
  • 11
  • 1
  • 2
    Please provide some sample data. You may also find it valuable to read [this question about good `R` questions](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – metasequoia Nov 08 '12 at 03:19

2 Answers2

2

Consider a grouped bar plot.

Modified example from 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")
Community
  • 1
  • 1
metasequoia
  • 7,014
  • 5
  • 41
  • 54
0

barplot() works well if you feed it a table. Consider the following data:

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))

First, let's make a two-dimensional table:

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

One barplot you could make of data with the disposition defined by tab would be this:

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

Which gives you the following: enter image description here

You can run ?barplot in order to learn how to further customize your plot.

Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107