-1

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.

enter image description here

empi75
  • 77
  • 1
  • 7
  • Welcome to Stack Overflow! Please do not provide your data as an image. No one wants to type it all in again. Instead, use `dput` to make a text version of your data that we can cut and paste from your question into R. – G5W Oct 21 '18 at 21:46
  • Please post the code that you have tried with. – Roman Oct 21 '18 at 21:49

1 Answers1

0

I found a different solution now, using ggplot. This works for me almost, only formatting is a bit off in case the number of bars for one group is different to another group. Then the label produced by geom_text is not centered in the bar. The rest works just fine.

In case somebody else needs this, here is the code I now use:

ggplot(PWS_K2_infos, aes(player_name, breakpoint_rate, fill = skill_type)) + 
geom_bar(stat="identity", position = position_dodge(preserve = "single"), width=0.9)+ 
geom_text(aes(label = round(breakpoint_rate, digits=2)), vjust=-0.3, position = position_dodge(0.9), size=3.5) +
geom_text(aes(label = paste("n=",n_serves)), vjust=1.6, color="white", position = position_dodge(0.9), size=3.5)+
ggtitle("Breakpoint% nach Serviceart") + 
xlab("") + ylab("Breakpunkt %") + 
scale_fill_brewer(palette = "Dark2")
empi75
  • 77
  • 1
  • 7