0

How I can do a multi boxplot in R with the heights of basketball player divided by position (G, F, C) for decade My dataset called player is this:

name = c("Kareem", "MJ", "Lebron")
year_start=c(1970, 1985, 2003)
year_end=c(1989, 2003, 2018)
position=c("C", "G", "F")
height=(219, 198, 203)
player<-data.frame(name, year_start, year_end, position, height)

my idea is like this

enter image description here

help me please

Mr Alsi
  • 37
  • 6
  • Hi and welcome to SO ! instead of pasting your dataset as an image, please provide a reproducible example of your dataset that everyone can copy/paste it will makes things easier for people to help you. See: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Apr 12 '20 at 16:24
  • have you try `ggplot(player, aes(x = position, y = height)) + geom_boxplot()` ? – dc37 Apr 12 '20 at 16:25
  • Sorry, I don't wrong anymore, your solution works, by my idea is divide the year in decade and for every decade have the boxplot for G-F-C (i didn't say it, sorry again), my ide a for divide the years is: year_start<=1959 & year_end>=1950 – Mr Alsi Apr 12 '20 at 16:39
  • Ok, I see. so please update your question with a reproducible example according my first comment. – dc37 Apr 12 '20 at 16:42
  • now is ok? sorry but I'm clumsy – Mr Alsi Apr 12 '20 at 17:11

1 Answers1

0

Your reproducible example is a little bit too short so I made one allowing to have each group for each decades

So, if you want to represent the boxplot of the height of players per decade you need first to attribute a decade to each players.

You can do it for example by rounding the starting year to the floor decade by doing:

player <- data.frame(name = LETTERS[1:20],
                     year_start = c(sample(1970:1979,10, replace= TRUE),
                                    sample(1980:1989,10,replace = TRUE)),
                     height = sample(180:220,20, replace= TRUE),
                     position = sample(c("P","G","C"),20, replace = TRUE))

player$decade = floor(player$year_start  %/% 10) * 10

Now, you can use it to create your boxplot as follow:

library(ggplot2) 

ggplot(player, aes(x = as.factor(decade), y = height, fill = position))+
  geom_boxplot()

enter image description here

Does it answer your question ?

dc37
  • 15,840
  • 4
  • 15
  • 32