I am trying to create a graph in R that looks similar to a Gantt chart (see the attached image for reference) but with a variable Y-axis for the amount of minutes a player has played.
I've tried the standard plot but it only allows for one X-axis value for each player.
The the dataset is below.
player age_start age_end minutes
1 Alex Wilkinson 31.88227 32.73101 2587
2 Bobo 31.61670 32.32307 2338
3 Danny Vukovic 31.31280 32.11225 2550
4 Michael Zullo 27.80287 28.65161 2550
5 Milos Ninkovic 30.55441 32.36413 2446
6 Filip Holosko 31.50171 33.30322 2009
7 Alex Brosque 30.71869 33.56879 2175
8 Rhyan Grant 17.84805 26.19302 2460
9 Brandon O'Neill 21.21834 23.06913 2210
10 Joshua Brillante 23.29911 24.11773 2289
11 Matt Simon 29.52772 31.28816 279
12 David Carney 32.17522 33.43463 796
13 Bernie Ibini 23.84668 24.64887 515
14 Milos Dimitrijevic 29.91923 33.22108 851
15 Jordy Buijs 28.04381 28.35592 1076
16 Matthew Jurman 23.56194 27.07187 901
17 Sebastian Ryall 19.95346 27.80287 336
18 Andrew Redmayne 27.96988 28.31211 748
19 Aaron Calver 18.46680 21.31691 464
20 George Blackwood 18.07255 19.92334 72
21 Nicola Kuleski 19.14853 20.99932 10
CodePudding user response:
If you supply your code, then it'll be easier to coach into what else to try. Though, from your question, I assume you're using geom_point()
, while what will work is geom_segment()
, when you can specify start and finish.
As a start, try (where df
is the data):
df %>%
ggplot()
geom_segment(aes(x = age_start, xend = age_end, y = player, yend = player), lwd = 2, colour = "magenta")
I didn't see how the percentages were derived, but these would be the y and yend.
CodePudding user response:
You can do this in ggplot using geom_segment
. A simplified version of your data and plot (without color, because I don't know what is used as gradient color variables):
set.seed(9)
n<-10 #number of player
player<-letters[1:n] #player names
age_start<-rnorm(n,20,3)
age_end<-age_start runif(n,1,10)
minutes<-runif(n,10,3000)
df<-data.frame(player,age_start,age_end,minutes)
library(ggplot2)
ggplot(data=df,aes(x=age_end,y=minutes))
geom_segment(aes(x=age_start,xend=age_end,y=minutes,yend=minutes))
geom_text(aes(label = player, x = age_end, y = minutes, hjust = -.5))
xlab("age")
See, plot.