Veli K
Veli K

Reputation: 35

Multiple legends in multiple graphs which is grouped bar line in R

When I looked my old projects, I noticed there is no code for a graph I included in my report. Can you please help me how I get the graph below?

The data table,

Score variable Actual Pois
0 away 0.316 0.314
0 home 0.228 0.213
1 away 0.366 0.363
1 home 0.313 0.329
2 away 0.205 0.210
2 home 0.248 0.254
3 away 0.081 0.081
3 home 0.137 0.131
4 away 0.016 0.023
4 home 0.055 0.050
5 away 0.009 0.005
5 home 0.003 0.015
6 away 0.003 0.001
6 home 0.006 0.004

The graph that I want to get,

The required graph

Thank you, Veli

Upvotes: 0

Views: 59

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

This gets you fairly close

library(ggplot2)

ggplot(within(df, variable <- factor(variable, c("home", "away"))),
       aes(Score, Actual, fill = variable, color = variable)) +
  geom_col(position = position_dodge(width = 0.9), color = NA) +
  geom_line(aes(y = Pois), position = position_dodge(width = 0.9),
            linewidth = 1.5) +
  geom_point(aes(y = Pois), position = position_dodge(width = 0.9),
             fill = NA, size = 4) +
  scale_fill_manual("Actual", values = c("#ffbca1", "#62c9c3")) +
  scale_color_manual("Poisson", values = c("#cd5c5c", "#006400")) +
  scale_x_continuous(breaks = 0:6) +
  labs(x = "Goals per match", y = "Proportion of matches",
       title = "Number of Goals per Match (TSL 2015-2016 Season)") +
  guides(colour = guide_legend(order = 1)) +
  theme_classic(base_size = 20) +
  theme(text = element_text(face = "bold"),
        legend.position = c(0.75, 0.85),
        legend.box.background = element_rect(color = "black", fill = "white"),
        legend.background = element_blank(),
        legend.box = "horizontal",
        panel.border = element_rect(fill = NA, color = "black"),
        legend.key.width = unit(1.5, "cm"))

enter image description here

Upvotes: 1

Related Questions