Home > Net >  Why is my dumbell plot not increasing with xend?
Why is my dumbell plot not increasing with xend?

Time:04-11

This is my dataframe:

mydf <- structure(list(NOME = c(1, 1, 6, 6, 5, 5, 3, 3, 9, 9), PLAYER.POSITION = c(
  "LATERAL",
  "LATERAL", "EXTREMO", "EXTREMO", "MEIOCAMPO", "MEIOCAMPO", "CENTRAL",
  "CENTRAL", "AVANCADO", "AVANCADO"
), Time = c(
  "PRIMEIRA PARTE",
  "SEGUNDA PARTE", "PRIMEIRA PARTE", "SEGUNDA PARTE", "PRIMEIRA PARTE",
  "SEGUNDA PARTE", "PRIMEIRA PARTE", "SEGUNDA PARTE", "PRIMEIRA PARTE",
  "SEGUNDA PARTE"
), values = c(
  104L, 81L, 108L, 100L, 101L, 100L,
  95L, 90L, 96L, 51L
)), row.names = c(NA, -10L), class = c(
  "tbl_df",
  "tbl", "data.frame"
))

library(ggalt)

df_final <- mydf %>% pivot_wider(names_from = Time, values_from = values)

ggplot(
  data = df_final,
  aes(
    x = 1, xend = 2,
    y = `PRIMEIRA PARTE`, yend = `SEGUNDA PARTE`,
    label = NOME
  )
)  
  geom_dumbbell(
    colour_x = "#5b8124", colour_xend = "#bad744",
    size_xend = 5, size_x = 12,
    dot_guide = TRUE, dot_guide_size = 0.25
  )

Why the lines are paralell? This should follow the increase/decrease between PRIMEIRA PARTE and SEGUNDA PARTE columns right?

I think I did something wrong with groupargument.

CodePudding user response:

Your description is unclear, but this might be closer to what you had in mind:

ggplot(data = df_final ,
       aes(y = PLAYER.POSITION, yend = PLAYER.POSITION,
           x = `PRIMEIRA PARTE`, xend = `SEGUNDA PARTE`,
           label =  NOME))  
  geom_dumbbell(colour_x = "#5b8124", colour_xend = "#bad744",
                size_xend = 5,size_x = 12,
                dot_guide=TRUE, dot_guide_size=0.25)   
  geom_text(colour = 'white')  
  labs(x = NULL)

enter image description here

CodePudding user response:

If you want to actually "see" the decrease and increase, geom_dumbbell is not a good choice, you can e.g. use a combination of geom_segment and geom_point:

ggplot(data = df_final)  
  geom_segment(aes(x = 1, xend = 2, y = `PRIMEIRA PARTE`, yend = `SEGUNDA PARTE`))  
  geom_point(aes(x = 1, y = `PRIMEIRA PARTE`), color = "#5b8124", size = 12)  
  geom_point(aes(x = 2, y = `SEGUNDA PARTE`), color = "#bad744", size = 5)   
  geom_text(aes(x = 1, y = `PRIMEIRA PARTE`, label = NOME), color = "white")

enter image description here

  • Related