Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6007999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:45:44+00:00 2026-05-23T01:45:44+00:00

I am trying to do some analysis of the recent MLB draft with some

  • 0

I am trying to do some analysis of the recent MLB draft with some ggplots in R

selection <- draft[c("Team","Division","Position")]
head(selection)

  Team   Division Position
1  pit NL Central        P
2  sea AL West           P
3  ari NL West           P
4  bal AL East           P
5  kc  AL Central        O
6  was NL East           I

where P = Pitcher , O=Outfield etc.

I want to show the number of players selected by team by position within each division

p <- ggplot(data=selection, aes(x=Team, fill= Position))  + geom_bar(position="stack")
p <-  p + coord_flip()
p <- p+ ylab("Players Selected")
p <- p + facet_wrap(~Division)
p

This gets me part of the way there but is very unattractive

a) the groupings work but all teams are shown in each divison grid – even though only the 5 or 6 team in each division actually – and correctly – show data

b) With the co-ord flip, the teams are listed in reverse alphabetical order down page. can I resort. It would also be nice to have left justification

c) How do i set the legend to Pitching, Outfield rather than P and O – is this a vector i somehow need to set and include

d) It would also be interesting to see the proportion of each teams selection committed to each type of player. This is accomplished by setting position= “fill”. Can i set the axes to % rather than 0 to 1. I also tried setting a geom_vline(aes(xintercept=0.5) -and yintercept in case the flip factored in –
but the line did not appear at halfway mark along the x axis

Help much appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:45:44+00:00Added an answer on May 23, 2026 at 1:45 am

    edit: complete revamping, including info from other answer, after grabbing the data (and storing it in a text file called mlbtmp.txt) and some more experimentation:

    selection <- read.table("mlbtmp.txt",skip=1)
    names(selection) <- c("row","League","Division","Position","Team")
    ## arrange order/recode factors
    selection <- transform(selection,
           Team=factor(Team,levels=rev(levels(Team))),
                       Position=factor(Position,levels=c("P","I","C","O"),
                                      labels=c("Pitching","Infield",
                                        "Center","Outfield")))
    

    I played around with various permutations of facet_grid, facet_wrap, scales, coord_flip, etc.. Some worked as expected, some didn’t:

    library(ggplot2)
    p <- ggplot(data=selection, aes(x=Team, fill= Position))  +
      geom_bar(position="stack")
    p + facet_grid(.~Division,scales="free_x") + coord_flip()  ## OK
    
    ## seems to fail with either "free_x" or "free_y"
    p + facet_grid(Division~.,scales="free") + coord_flip()
    
    ## works but does not preserve 'count' axis:
    p + facet_wrap(~Division,scales="free")
    

    I ended up with facet_wrap(...,scales="free") and used ylim to constrain the axes.

    p + facet_wrap(~Division,scales="free") + coord_flip() +
      ylim(0,60) + opts(axis.text.y=theme_text(hjust=0))
    

    mlb1

    In principle there might be a way to use ..density.., ..ncount.., ..ndensity.., or one of the other statistics computed by stat_bin instead of the default ..count.., but I couldn’t find a combination that worked.

    Instead (as is often the best solution when stuck with ggplot’s on-the-fly transformations) I reshaped the data myself:

    ## pull out Team identification within Division and League
    stab <- unique(subset(selection,select=c(Team,Division,League)))
    ## compute proportions by team
    s2 <- melt(ddply(selection,"Team",function(x) with(x,table(Position)/nrow(x))))
    ## fix names
    s2 <- rename(s2,c(variable="Position",value="proportion"))
    ## merge Division/League info back to summarized data
    s3 <- merge(s2,stab)
    
    p2 <- ggplot(data=s3, aes(x=Team, fill= Position,y=proportion))  +
      geom_bar(position="stack")+scale_y_continuous(formatter="percent")+
      geom_hline(yintercept=0.5,linetype=3)+ facet_wrap(~Division,scales="free") +
      opts(axis.text.y=theme_text(hjust=0))+coord_flip()
    

    mlb2

    There’s obviously a little more prettying-up that could be done here, but this should get you most of the way there …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function to do some often repeated analysis, and one
I'm trying to do some network analysis for a client. The provided road-network GIS
i'm trying to do age analysis on some data and need to do a
I'm trying to do some analysis and when writing down a list of functionalities
I was trying to do some analysis on calling start and join simultaneously, //Starting
I am new to R and trying to do some correlation analysis on multiple
I have a table where I'm trying to pull some trend analysis from where
I'm trying to aggregate some customer order data into one table for analysis. The
I’m thinking about trying some development for the iPhone, is it possible to install
I'm trying some of the ASP.NET MVC tutorials and one of them has the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.