0

I am trying to show the minutes played by a football team using a circular barplot. But the labels are not linear, like I need them to be in a straight line. Below is the entire code

library(tidyverse)

df <- read_csv("F:/Others/football analytics/realmadrid/mins.csv")


data <- data.frame(
  individual= df$Player,
  group=c( rep('A', 1), rep('B', 12), rep('C', 6), rep('D', 8)) ,
  value=df$Min
)


data$id <- seq(1, nrow(data))
label_data <- data
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.7) /number_of_bar    
label_data$hjust <- ifelse( angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle+180, angle)

ggplot(data, aes(x=id, y=value, fill=group)) +
  geom_bar(stat="identity", alpha=0.5) +
  ylim(-100,120) +
  theme_minimal() +
  theme(
    legend.position = "none",
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank()
  ) +
  coord_polar() + 
  geom_text(data=label_data, aes(x=id, y=value + 5, label=individual, hjust = hjust), color="black", fontface="bold",alpha=0.6, size=2.5,angle= label_data$angle, inherit.aes = FALSE) 

And this is the output I get

output

Could anyone help? Thanks alot!

Edit : the input is in the below format

Player  Mins

a       20

b       30 

c       40

d       50

e       60
Pavan
  • 17
  • 5
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. We cannot use "mins.csv" since we do not have it in our machines. It's better to share data as a `dput()` so we can just copy/paste from the question itself. We don't need your actual data, just enough so that we can recreate a similar plot. – MrFlick May 26 '21 at 17:35
  • @MrFlick thanks for that, just updated the question. – Pavan May 26 '21 at 17:44
  • in geom_text() change the angle argument to angle = 0 – Susan Switzer May 26 '21 at 17:53
  • @SusanSwitzer but then the labels get overlapped and they are not aligned with the bar, I want the label to be aligned with the bar; [when angle is zero](https://imgur.com/quGeI0k) – Pavan May 26 '21 at 18:26
  • What do you mean they are not aligned with the bar? It's unclear what exactly you want this to look like. It sounds like you are going to have to place the labels manually unless you can mathematically describe where you want them to go. – MrFlick May 26 '21 at 18:48
  • @MrFlick [this is the expected output](https://imgur.com/a/icnS3l2) ; in the output I get from the code. the labels are not linear – Pavan May 26 '21 at 19:25

0 Answers0