Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a double bar contrast plot using ggplot2

Tags:

r

ggplot2

I am trying to replicate a plot using ggplot2 to show the performance between two soccer teams. I look for getting this plot:

enter image description here

I have the data df for that. This is the code I have been using:

library(ggplot2)
#Plot
ggplot(df,aes(x=type,y=value,fill=name))+
  geom_bar(stat = 'identity')+
  geom_text(aes(label=abs(value),hjust=1))+
  coord_flip()

Which produces next result:

enter image description here

The result is far away from first plot.

The issues I face are next:

  1. The scale is too large for some type values, so is not possible to see each measure with independent scale. I think a facet_grid() or facet_wrap() would solve this but it did not worked.

  2. The values for type must go in the middle, but I do not know how to move the axis to middle.

  3. The labels for each bar should be on top of each bar but when I adjust hjust one of them goes to the right place but the other is placed wrong.

  4. Finally, in df there is column named logo which stores the flags of two teams, how I can add the flags for each team one at top right side and the other at top left side.

Many thanks. This is the data df.

#Data
df <- structure(list(type = c("Shots on Goal", "Shots off Goal", "Total Shots", 
"Blocked Shots", "Shots insidebox", "Shots outsidebox", "Fouls", 
"Corner Kicks", "Offsides", "Ball Possession % ", "Yellow Cards", 
"Red Cards", "Goalkeeper Saves", "Total passes", "Passes accurate", 
"Passes %", "Shots on Goal", "Shots off Goal", "Total Shots", 
"Blocked Shots", "Shots insidebox", "Shots outsidebox", "Fouls", 
"Corner Kicks", "Offsides", "Ball Possession % ", "Yellow Cards", 
"Red Cards", "Goalkeeper Saves", "Total passes", "Passes accurate", 
"Passes %"), value = c(7, 2, 13, 4, 10, 3, 9, 8, 2, 78, 0, 0, 
1, 797, 716, 90, -3, -4, -8, -1, -6, -2, -14, 0, -2, -22, -2, 
0, -1, -215, -142, -66), name = c("England", "England", "England", 
"England", "England", "England", "England", "England", "England", 
"England", "England", "England", "England", "England", "England", 
"England", "Iran", "Iran", "Iran", "Iran", "Iran", "Iran", "Iran", 
"Iran", "Iran", "Iran", "Iran", "Iran", "Iran", "Iran", "Iran", 
"Iran"), logo = c("https://media.api-sports.io/football/teams/10.png", 
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png", 
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png", 
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png", 
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png", 
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png", 
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png", 
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/10.png", 
"https://media.api-sports.io/football/teams/10.png", "https://media.api-sports.io/football/teams/22.png", 
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png", 
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png", 
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png", 
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png", 
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png", 
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png", 
"https://media.api-sports.io/football/teams/22.png", "https://media.api-sports.io/football/teams/22.png", 
"https://media.api-sports.io/football/teams/22.png")), row.names = c(NA, 
-32L), class = c("tbl_df", "tbl", "data.frame"))
like image 568
user007 Avatar asked Oct 29 '25 14:10

user007


1 Answers

Perhaps something like this?

library(tidyverse)
library(ggimage)

df %>%
  group_by(type) %>%
  mutate(tot = sum(abs(value)),
         prop = value/tot) %>%
  ggplot(aes(prop, type, color = name)) +
  geom_linerange(aes(xmin = -1, xmax = 1), color = "gray95", linewidth = 3) +
  geom_linerange(aes(xmin = prop, xmax = 0), linewidth = 3) +
  scale_color_manual(values = c("#8ded05", "#00aaff")) +
  geom_text(aes(label = type, x = 0), check_overlap = TRUE, nudge_y = 0.4,
            color = "black") +
  geom_text(aes(x = ifelse(name == "England", 1.05, -1.05), 
                label = abs(value)), color = "black") +
  theme_void() +
  scale_y_discrete(expand = c(0.1, 0)) +
  annotate(geom = "text", x = c(-0.5, 0.5), y = c(17, 17),
           label = c("Iran", "England"), size = 5) +
  geom_image(data = data.frame(x = c(1, -1), y = 17, 
                               image = unique(df$logo)),
             aes(x, y, image = image), inherit.aes = FALSE)+
  guides(color = guide_none())

enter image description here

like image 125
Allan Cameron Avatar answered Oct 31 '25 04:10

Allan Cameron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!