I have a tibble (see below) that I read out of a file. I would want to barplot from the tibble in the following way:
The x axis should be grouped by player_name
and skill_type
should have the same colour. The height of the bar should equal the breakpoint_rate
. At the bottom should be the number n_serves
for each bar.
I tried to convert to a matrix or a dataframe but nothing worked. As I am new to R, I struggle at so many details that I decided to ask here.
Here is the code for the tibble that dput
gave:
structure(list(player_name = c("John HYDEN", "John HYDEN", "Nikita
LIAMIN", "Theodore BRUNNER", "Viacheslav KRASILNIKOV", "Viacheslav
KRASILNIKOV"), skill_type = c("Jumpfloat", "Standing serve", "Jumpfloat",
"Jumpfloat", "Jump serve", "Jumpfloat"), n_serves = c(15L, 3L, 24L, 14L,
16L, 1L), breakpoint_rate = c(0.4, 0, 0.583333333333333,
0.285714285714286, 0.375, 0)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -6L), vars = "player_name", drop = TRUE)
code that I tried:
## Attempt with conversion to matrix
PWS_mat <- t(as.matrix(PWS_K2_infos))
barplot(`colnames<-`(t(PWS_mat[-c(1,2)]), PWS_mat[,2]), beside=TRUE,
legend.text = TRUE, col = c("red", "green"),
args.legend = list(x = "topleft", bty = "n", inset=c(-0.05, 0)), xlab="Serviceart", ylab="Breakpointchance")
that leads to an error...
## Attempt with conversion to dataframe
PWS_df <- as.data.frame(select(PWS_K2_infos,-n_serves))
barplot(`colnames<-`(t(PWS_df[-c(1,2)]), PWS_df[,2]), beside=TRUE,
legend.text = TRUE, col = c("red", "green"),
args.legend = list(x = "topleft", bty = "n", inset=c(-0.05, 0)), xlab="Serviceart", ylab="Breakpointchance")
this one gives me a barplot, but I I do not get it grouped the way I want it.